JS笔记

71 阅读1分钟

jQuery移动滚轮到指定元素:

$("body,html").animate({scrollTop:$("."+id+"").offset().top-100});

js移动滚轮到指定元素:

const openBtn = document.getElementById("btn");  
const box = document.getElementById("box");
openBtn.addEventListener("click", () => box.scrollIntoView({ behavior: "smooth" }));

js移动滚动监听元素出现在可视窗口:

//监听滚动
$(function(){ 
   var length = $('.con_left ul li').length;
    $(document).scroll(function(){ 
       for(let i =0;i<length;i++){  
          let top = $('.minbuilding').eq(i).offset().top;//获取当前元素离顶部的距离    
          let scrop = $(document).scrollTop();//获取页面滚动条离顶部的距离 
          if(scrop+120>top){          
               $('.con_left ul li').removeClass('leftred')     
               $('.con_left ul li').eq(i).attr('class','leftred')     
          }       
       }    
    })
})