简体   繁体   English

如何重置slidetoggle()

[英]how to reset slidetoggle()

I am working on a responsive site that with the aid of twitter bootstrap, in this case it takes the footer heading and when the screen width is a certain width , it then turns on slideToggle() , this works fine, but the problem is when I increase the browser size again I want this functionality to not only turn off but to reset back display:block . 我正在使用Twitter Bootstrap的响应网站上进行工作,在这种情况下,它需要页脚标题,并且当屏幕宽度为某个宽度时,然后打开slideToggle() ,这可以正常工作,但是问题是当我再次增加浏览器的大小,我希望此功能不仅可以关闭,而且可以重置为display:block

Im currently still learning jQuery and JS so it may be an obvious one. 我目前仍在学习jQuery和JS,因此可能很明显。

I need it to work so its not relying on a click to reset it back... 我需要它工作,因此它不依赖于单击即可将其重置回...

Here is the entire code including getting screen size: 这是完整的代码,包括获取屏幕尺寸:

        var myWidth = 0, myHeight = 0;
    function getSize(){
            if( typeof( window.innerWidth ) == 'number' ) {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
                }   
    }

    getSize(); // run first time
            $(window).resize(function(){
                getSize(); // do it on resize
            });

            $("#footer h3").click(function () {

                if (myWidth < 980) {
                $(this).toggleClass("active");
                $(this).parent().find("ul").slideToggle('medium');  
                }                   
            });

Im thinking slideDown() but not sure how to make it happen automatically. 我在想slideDown()但不确定如何使其自动发生。

Inside the code that checks for screen width (maybe you can post that anyway?) you can do something like this: 在检查屏幕宽度的代码内(也许您可以张贴它吗?),您可以执行以下操作:

if (myWidth < 980) {
    $(this).parent().find("ul").slideDown('medium');
} else {
    $(this).parent().find("ul").slideUp('medium');
}

For more control over the slide behavior it's better to use the separate slideDown and slideUp methods instead of slideToggle. 为了更好地控制幻灯片的行为,最好使用单独的slideDown和slideUp方法而不是slideToggle。 In that case you know for sure your ul gets slide up/down instead of just toggled. 在这种情况下,您可以确定您的ul会向上/向下滑动,而不是只是上下滑动。

Also, you might want to add the stop() method to avoid buildup of slide animations if the window width passes the 980 threshold multiple times in short succession: 另外,如果窗口宽度连续短时间多次超过980阈值,则可能需要添加stop()方法来避免幻灯片动画的生成:

if (myWidth < 980) {
    $(this).parent().find("ul").stop().slideDown('medium');
} else {
    $(this).parent().find("ul").stop().slideUp('medium');
}

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

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