【问题标题】:jQuery - Recalculate variable on window resize and re-run functionjQuery - 在窗口调整大小和重新运行功能时重新计算变量
【发布时间】:2012-12-13 23:22:16
【问题描述】:

我写了一个隐藏和切换顶栏 div 的函数:

jQuery(document).ready(function($) {

        $.fn.myTopBar = function() {

                var sipPosTop = 0;
                var topDivHeight = $("#top").outerHeight();

                $('#top').css('top' , -topDivHeight+10).show().animate({top: '+=0'}, 2000);


                $("#top-toggle").click(function(e) {
                    e.preventDefault();
                    $("#top").animate({ top: sipPosTop }, 200, 'linear', function() {
                        if(sipPosTop == 0) { sipPosTop = -topDivHeight+10; }
                        else { sipPosTop = 0; }
                    });
                });
        };
});

在页面加载时效果很好,但在窗口调整大小时,#top div 高度会发生变化,并且会破坏布局。如何重新计算它并在调整窗口大小时重新运行该函数?我尝试了以下方法:

jQuery(document).ready(function($){
    $.fn.myTopBar();
});

jQuery(window).resize(function($){
    $.fn.myTopBar();
});

但不起作用。感谢您的帮助

【问题讨论】:

    标签: jquery toggle window-resize outerheight


    【解决方案1】:

    编辑 -- 根据您的 cmets,我编写了这个插件,(希望)将具有您正在寻找的功能。如果有任何事情,您也许可以提取一些关于如何去做的信息。

    用这个调用插件 --

    $(function () {
        $('.someFlyoutContainerClass').flyout({
            flyButton : $('.someButtonClass'), // button that toggles the flyout
            topOffset : 50, // (px) offset from the top of the window
            fade : { // (fade speeds/durations) -- remove object if not needed
                inSpeed : 350,
                easingIn : 'swing', // by removing easing, it will default to linear
                outSpeed: 250,
                easingOut : 'swing'            
            },
            slide : { // (slide speeds/durations) -- remove object if not needed
                inSpeed : 350,
                easingIn : 'swing',
                outSpeed : 250,
                easingOut : 'swing'      
            },
            closeButton : $('.close-flyout'), // you may remove if not needed
            clickOffClose: true // click anywhere but modal/flyout to close it
        });
    });
    

    这是脚本的工作版本——(12/13 更新) http://jsfiddle.net/jmsessink/fC8ht/5/

    我希望这会有所帮助,如果您有任何问题,请告诉我。

    【讨论】:

    • 对不起,我在上一部分写了两次相同的代码,我现在已经编辑了。我已经尝试了很多方法。不工作我不知道为什么。
    • 试试这样 - jQuery(window).resize($.fn.myTopBar);​
    • 我试过了,但也没有用。顶部栏有一些奇怪的行为,当我调整窗口大小时它会上下跳跃,当我停止切换时,它根本不起作用。
    • 嗯,从逻辑上讲,这就是它的设置;对于窗口调整大小上的每个移动像素,该函数将触发并切换元素的顶部位置——因此来回切换会导致“奇怪的行为”。在那种情况下,你到底想完成什么,因为这可能会帮助我弄清楚如何调整脚本。
    • 我只注意到一个小问题,由于某种原因,如果我向下滚动页面,然后单击顶部切换按钮将其打开,则切换不会打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2013-02-19
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多