gpt4 book ai didi

php - $.scrollTo 在 Chrome 中不工作

转载 作者:搜寻专家 更新时间:2023-10-31 20:58:23 25 4
gpt4 key购买 nike

我制作了一个简单的 WordPress 网站,主题名为 OneEngine。

这是一个演示链接:https://oneengine.enginethemes.com/

有趣的是,在 Chrome 中导航似乎不起作用。

我已经确定了负责滚动的脚本片段。

// SCROLL TO
$('#main-menu-top a,ul.slicknav_nav li a').click(function(event){
event.stopPropagation();
event.preventDefault();

console.log('Click event');

if($(this).hasClass('active'))
return;

$('#main-menu-top a').removeClass('active').css('border-bottom-color', 'none');

$('ul.slicknav_nav li a').removeClass('active');

$(this).addClass('active');

if(this.hash == "#home")
$.scrollTo(0,800);
else
$.scrollTo( this.hash, 800, {offset:-$(".sticky-wrapper").height()});

var bgcolor = $(this.hash).find('span.line-title').css('backgroundColor');

$(this).css('border-bottom-color', bgcolor);

$('.slicknav_nav').hide('normal', function() {

$(this).addClass('slicknav_hidden');

});

$('a.slicknav_btn').removeClass('slicknav_open').addClass('slicknav_collapsed');

return false;
});

$("a#scroll_to").click(function(event) {
$.scrollTo("#header", 800);
});

我确信不起作用的部分是 $.scrollTo();。在 Google 中输入它会产生 window.scrollTo,所以我将每个实例都更改为 window.scrollTo(),但这会产生没有偏移的跳动 anchor 。

这部分 offset:-$(".sticky-wrapper").height() 我知道大致类似于:

var stickyWrapperHeight = $(".sticky-wrapper").height()(即 76 像素)。

我实际上找不到所描述的 native scrollTo 方法,所以我认为它实际上可能是 JQuery scrollTo - https://github.com/flesler/jquery.scrollTo

我最大的问题是为什么这在 Chrome 中不起作用?

我已经看到了潜在的替代方案,例如:

$("a").on('click', function(event) {

console.log($(".sticky-wrapper").height())

var height = $(".sticky-wrapper").height();

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});

我只是在使用过时版本的 scrollTo() 吗?

最佳答案

即使他们的演示页面上存在 SSL 问题,并且如果您不键入 jQuery() 相反,您得到的“$”也不是一个函数。我注意到即使您尝试在控制台中直接使用该插件,它仍然无法正常工作。

他们在主题中使用的 scrollTo 版本已经过时,并且在最新版本的 chrome 中也不起作用:

https://github.com/flesler/jquery.scrollTo/issues/164

尝试使用最新版本,它应该可以工作。

否则,如果由于某种原因你不能更新插件,你可以使用类似这样的东西让它工作:

jQuery('html,body').animate({scrollTop: jQuery("#skills").offset().top}, 'slow');

关于php - $.scrollTo 在 Chrome 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50273599/

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