当URL更改滚动到某个哈希时,将类添加到DIV

发布于 2025-01-24 16:10:31 字数 1213 浏览 1 评论 0 原文

我的URL hash更改scroll 从

等等。这是一个完整的滚动效果。

想做的是在URL上的滚动变为#-2-2

$(document).ready(function () {

        if (window.location.href.indexOf("emotion--2") > -1) {
            $('#shirt').addClass('draw-shirt');
        }
    });
        
        

$(window).scroll(function () {
    
        function locationHashChanged() {
            if(window.location.href.indexOf("emotion--2") > -1) {
                $('#shirt').addClass('draw-shirt');
             }
        }

    });

。 IS www.website.com#section-section--2--2- in Load on Load,但不在滚动上。

该代码

function isScrolledIntoView(elem) {...etc... 

由于完整的滚动效应,

在这里无法使用。感谢您的帮助。

My URL hash changes on scroll from

and so on. It's a fullpage-scrolling effect.

What I want to do is to add the class .draw-shirt to the div #shirt when the URL changes on scroll to #section--2.

I tried this code:

$(document).ready(function () {

        if (window.location.href.indexOf("emotion--2") > -1) {
            $('#shirt').addClass('draw-shirt');
        }
    });
        
        

$(window).scroll(function () {
    
        function locationHashChanged() {
            if(window.location.href.indexOf("emotion--2") > -1) {
                $('#shirt').addClass('draw-shirt');
             }
        }

    });

It's working, if the URL is www.website.com#section--2 on load but not on scroll.

The code

function isScrolledIntoView(elem) {...etc... 

is not working here because of the fullpage-scrolling-effect.

Thanks for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2025-01-31 16:10:31

我认为您可以使用“ nofollow noreferrer”>本地窗口对象的hashchange 事件。

window.addEventListener('hashchange', function() {
    if (location.hash === '#emotion--2') {
        // your code to add class
   } 
});

I think you could use the native hashchange event of the window object.

window.addEventListener('hashchange', function() {
    if (location.hash === '#emotion--2') {
        // your code to add class
   } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文