gpt4 book ai didi

javascript - 向上和向下调整滚动位置到最近的包装器 div

转载 作者:行者123 更新时间:2023-12-03 12:04:57 26 4
gpt4 key购买 nike

我已经阅读了很多线程,但仍然无法找到不使用快速滚动插件的解决方案( https://github.com/wtm/jquery.snapscroll )。

我试图遵循这个

function scrollTo(a){
//Get current scroll position
var current = (document.all ? document.scrollTop : window.pageYOffset);
//Define variables for data collection
var target = undefined;
var targetpos = undefined;
var dif = 0;
//check each selected element to see witch is closest
$(a).each(function(){
//Save the position of the element to avoid repeated property lookup
var t = $(this).position().top;
//check if there is an element to check against
if (target != undefined){
//save the difference between the current element's position and the current scroll position to avoid repeated calculations
var tdif = Math.abs(current - t);
//check if its closer than the selected element
if(tdif < dif){
//set the current element to be selected
target = this;
targetpos = t;
dif = tdif;
}
} else {
//set the current element to be selected
target = this;
targetpos = t;
dif = Math.abs(current - t);
}
});
//check if an element has been selected
if (target != undefined){
//animate scroll to the elements position
$('html,body').animate({scrollTop: targetpos}, 2000);
}

}

我正在尝试让它滚动到 View 中

<div class="projectWrap"> 

fiddle :http://jsfiddle.net/Dar_T/2h2wjp2L/1/

很像这个网站 http://www.takumitaniguchi.com/tokyoblue/让它滚动查看其容器。

最佳答案

首先您必须找到元素的offset()。然后将其与 $(window.scrollTop()) 进行比较,然后您可以进行任何您想要的更改。以下是一些代码:

var project1 = $(".projectWrap").offset();
if($(window).scrollTop() >= project1.top){ // compare window scrolltop to element offset()
$("#tlos1").css("color","blue"); // change navigation
$("#tlos2").css("color","black");
$("#tlos3").css("color","black");
}

这是DEMO

关于javascript - 向上和向下调整滚动位置到最近的包装器 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25225367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com