jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – window.location.hash – 停止在chrome中重新加载大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下问题:

$('.gotoservices').click(function(){
    $('html,body').scrollTo($("#services"),1000);
    window.LOCATIOn.hash = "services";
    return false;
});

代码有效,但由于某种原因,页面在scrollTo之前闪烁.

如果我删除window.LOCATIOn.hash行返回false;工作,页面不闪烁/闪烁.

我试过e.preventDefault – 不起作用

我很难找到任何解决方法.

干杯

解决方法

在一般意义上,您可以使用HTML5历史记录更新URL,而无需浏览器重新呈现任何内容

history.pushState(null,null,'#myhash');

当然,您可能希望为旧浏览器提供后备,并且您可能希望仅在完成动画后才能执行此操作.

因此,与您的代码集成:

$('.gotoservices').click(function(E){
    //Prevent default so the browser doesn't try to do anything
    e.preventDefault();
    var $hash = "#services";
    $('html,body').scrollTo($($hash),1000,function () {
        //If the browser supports HTML5 history,//use that to update the hash in the URL
        if (history.pushStatE) {
            history.pushState(null,$hash);
        }
        //Else use the old-fashioned method to do the same thing,//albeit with a flicker of content
        else {
            LOCATIOn.hash = $hash;
        }
    });
});

大佬总结

以上是大佬教程为你收集整理的jquery – window.location.hash – 停止在chrome中重新加载全部内容,希望文章能够帮你解决jquery – window.location.hash – 停止在chrome中重新加载所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。