简体   繁体   English

如果CSS具有特殊价值,请不要执行jQuery脚本

[英]Do not execute jQuery script if CSS is of particular value

On my website, I have a sidebar DIV on the left and a text DIV on the right. 在我的网站上,左侧有一个侧边栏DIV,右侧有一个文本DIV。 I wanted to make the sidebar follow the reader as he or she scrolls down so I DuckDuckGo'ed a bit and found this then modified it slightly to my needs: 我想让侧边栏跟随读者,当他或她向下滚动时,所以我做了DuckDuckGo,发现了一点,然后根据我的需要对其做了一些修改:

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(function(){

    var $sidebar = $('#sidebar'),
        sidebarOffset = $sidebar.offset(),
        $window = $(window),
        gap = $('#header').css('marginBottom').replace(/[^-\d\.]/g, ''),
        distance = ($window.scrollTop()) - (sidebarOffset.top - gap),
        footerHeight = $('#footer').outerHeight();

    $window.scroll(function(){
        distance = ($window.scrollTop()) - (sidebarOffset.top - gap);        
        if ( distance > 0 ) {
            $sidebar.css({'top': gap + 'px', 'position' : 'fixed'});
        } else {
            $sidebar.css({'top': '0', 'position': 'relative'});
        }

    })

});    

});//]]>  

</script>

And it works just like I want it to. 它就像我想要的那样工作。 However, my website uses Skeleton framework to handle responsive design. 但是,我的网站使用Skeleton框架来处理响应式设计。 I've designed it so that when it goes down to mobile devices (horizontal then vertical), sidebar moves from being to the left of the text to being above it so that text DIV can take 100% width. 我对它进行了设计,以便当它下降到移动设备(水平然后是垂直)时,边栏将从文本的左侧移动到其上方,以便文本DIV可以采用100%的宽度。 As you can probably imagine, this script causes the sidebar to cover parts of text as you scroll down. 您可能会想像到,当您向下滚动时,此脚本会导致边栏覆盖部分文本。

I am completely new to jQuery and I am doing my best through trial-and-error but I've given up. 我对jQuery完全陌生,我通过反复试验尽我所能,但我已经放弃了。 What I need help with is to make this script not execute if a certain DIV has a certain CSS value (ie #header-logo is display: none ). 我需要帮助的是,如果某个DIV具有某个CSS值(即, display: none #header-logo display: none ),则使该脚本执行。

Ideally, the script should check for this when user resizes the browser, not on website load, in case user resizes the browser window from normal size to mobile size. 理想情况下,脚本应在用户调整浏览器大小时(而不是在网站加载时)检查此情况,以防用户将浏览器窗口的大小从正常大小更改为移动大小。

I imagine it should be enough to wrap it in some IF-ELSE statement but I am starting to pull the hair out of my head by now. 我认为将其包裹在IF-ELSE语句中就足够了,但是现在我开始将头发拔出头来。 And since I don't have too much hair anyway, I need help! 而且由于我的头发也不多,因此我需要帮助!

Thanks a lot in advance! 在此先多谢!

$("#headerLogo").css("display") will get you the value. $("#headerLogo").css("display")将为您带来价值。

http://api.jquery.com/css/ http://api.jquery.com/css/

I also see you only want this to happen on resize, so wrap it in jquery's resize() function: 我还看到您只希望在调整大小时发生这种情况,因此将其包装在jquery的resize()函数中:

https://api.jquery.com/resize/ https://api.jquery.com/resize/

This function will execute on window resize and will check if #header-logo is visible. 此函数将在调整窗口大小时执行,并检查#header-logo是否可见。

$(window).resize(function() {
    if ($('#header-logo').is(':visible')) {
        // Your code
    }
});

I think you need to check this on load to, because you don't know if the user will start with mobile view or not. 我认为您需要在加载时进行检查,因为您不知道用户是否将以移动视图开始。 You could do something like this: 您可以执行以下操作:

$(window).resize(function() {
    if ($('#header-logo').is(':visible')) {
        // Your code
    }
}).resize();

This will get executed on load and on resize. 这将在加载和调整大小时执行。

EDIT: You will probably need to turn off the scroll function if #header-logo is not visible. 编辑:如果#header-logo不可见,您可能需要关闭滚动功能。 So, instead of create the function inside the scroll event, you need to create it outside: 因此,您需要在外部创建函数,而不是在scroll事件内部创建函数:

$(window).resize(function() {
    if ($('#header-logo').is(':visible')) {
        var $sidebar = $('#sidebar'),
            sidebarOffset = $sidebar.offset(),
            $window = $(window),
            gap = $('#header').css('marginBottom').replace(/[^-\d\.]/g, ''),
            distance = ($window.scrollTop()) - (sidebarOffset.top - gap),
            footerHeight = $('#footer').outerHeight();

        function myScroll() {
            distance = ($window.scrollTop()) - (sidebarOffset.top - gap);        
            if ( distance > 0 ) {
                $sidebar.css({'top': gap + 'px', 'position' : 'fixed'});
            } else {
                $sidebar.css({'top': '0', 'position': 'relative'});
            }
        }

        $window.on('scroll', myScroll);
    } else {
        $(window).off('scroll', myScroll);
    }
});

Didn't test it, but you get the idea. 没有测试,但是您知道了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM