【问题标题】:Reload jQuery Function after PrettyPhoto has been triggered触发 PrettyPhoto 后重新加载 jQuery 函数
【发布时间】:2011-04-22 18:28:06
【问题描述】:

我希望标题是不言自明的。无论哪种方式,我正在尝试在另一个进程被触发后重新加载(或重新读取)jQuery 中的一个函数(在本例中为 prettyPhoto 插件)。

这是我当前的 jQuery 函数:

        /** Scrolling Sidebar function **/
            var $sidebar   = $(".sideBar"),
                $window    = $(window),
                $mainImage = $(".mainImage"),
                offset     = $sidebar.offset(),
                threshold  = $mainImage.height() - $sidebar.height(),
                topPadding = 15;

    <?php if ( $options['scroll_sidebar'] == true ) { ?>
        if( $mainImage.height() >= 400 ) {
            $window.scroll(function() {
                if ($window.scrollTop() > threshold) {
                    if( $('.sideBar').has('.zoomImage').length ) {
                        $sidebar.stop().animate({
                            marginTop: threshold + 50
                        });
                    } else {
                        $sidebar.stop().animate({
                            marginTop: threshold + 60
                        });
                    }
                } else if ($window.scrollTop() > offset.top) {
                    $sidebar.stop().animate({
                        marginTop: $window.scrollTop() - offset.top + topPadding
                    });
                } else {
                    $sidebar.stop().animate({
                        marginTop: '40px'
                    });
                }
            });
        }
    <?php } ?>

当一个带有 fullSize 类的锚标签被点击时,prettyPhoto 会被触发。

我跟随this 线程使用 setTimeout 方法重新加载“代码块”。这就是我所做的:(将前面的代码包装在一个函数中,在该函数内部使用 setTimeout,然后在函数外部调用 setTimeout)

    /** Scrolling Sidebar function **/
function scrollSidebar() {
            var $sidebar   = $(".sideBar"),
                $window    = $(window),
                $mainImage = $(".mainImage"),
                offset     = $sidebar.offset(),
                threshold  = $mainImage.height() - $sidebar.height(),
                topPadding = 15;

    <?php if ( $options['scroll_sidebar'] == true ) { ?>
        if( $mainImage.height() >= 400 ) {
            $window.scroll(function() {
                if ($window.scrollTop() > threshold) {
                    if( $('.sideBar').has('.zoomImage').length ) {
                        $sidebar.stop().animate({
                            marginTop: threshold + 50
                        });
                    } else {
                        $sidebar.stop().animate({
                            marginTop: threshold + 60
                        });
                    }
                } else if ($window.scrollTop() > offset.top) {
                    $sidebar.stop().animate({
                        marginTop: $window.scrollTop() - offset.top + topPadding
                    });
                } else {
                    $sidebar.stop().animate({
                        marginTop: '40px'
                    });
                }
            });
        }

        $('a.fullSize').click(function() {
            setTimeout(scrollSidebar, 1000);
        });
<?php } ?>

    }
setTimeout(scrollSidebar, 1000);

很遗憾,这不起作用。那么,一旦触发了另一个动作/插件,您对如何重新加载/重新读取函数有什么想法吗?

谢谢!
克里斯

【问题讨论】:

    标签: jquery function reload


    【解决方案1】:

    " 在另一个进程被触发后,我正在尝试在 jQuery 中重新加载(或重新读取)一个函数"

    为什么要调用 setTimeout?这将在一定时间后触发函数调用,然后每 1000 毫秒循环/重复一次。如果你只是想重新加载函数,只需调用函数本身。

     $('a.fullSize').click(function() {
                scrollSidebar();
            });
    

    编辑: 你漂亮的图像可能会影响滚动条。但是,http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/documentation/ 具有回调属性。所以我喜欢这样(回调函数将在关闭 perryimaege 时被调用,因为你遇到的问题是当它漂亮的图像打开时。所以在这方面,这就是为什么点击事件没有解决你的问题(代码可能正在工作))图片关闭后你需要一些东西

    $("a[rel^='prettyPhoto']").prettyPhoto({
            allow_resize: true,
            theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
            callback: sideBar
        });
    

    并在其他地方声明您的侧边栏

    function sideBar()
    {
        var $sidebar   = $(".sideBar"),
        $window    = $(window),
        $mainImage = $(".mainImage"),
        offset     = $sidebar.offset(),
        threshold  = $mainImage.height() - $sidebar.height(),
        topPadding = 15;
    
        if( $mainImage.height() >= 400 ) {
            $window.scroll(function() {
                if ($window.scrollTop() > threshold) {
                    if( $('.sideBar').has('.zoomImage').length ) {
                        $sidebar.stop().animate({
                            marginTop: threshold + 50
                        });
                    } else {
                        $sidebar.stop().animate({
                            marginTop: threshold + 60
                        });
                    }
                } else if ($window.scrollTop() > offset.top) {
                    $sidebar.stop().animate({
                        marginTop: $window.scrollTop() - offset.top + topPadding
                    });
                } else {
                    $sidebar.stop().animate({
                        marginTop: '40px'
                    });
                }
            });
        }   
    }
    

    来自您网站http://jsfiddle.net/qeDwM/的工作代码

    【讨论】:

    • 不。 setInterval 执行您所说的操作,而不是 setTimeout。此外,我需要我的函数在 PrettyPhoto 被触发之前和之后工作。这就是为什么我需要在 prettyPhoto 被触发后重新加载。
    • @cr0z3r 我很糟糕,它看起来好像setTimeout(scrollSidebar, 1000);scrollSidebar() 函数内部,在这种情况下它将每 1000 毫秒重复一次,因此表现得像 setIntevral。至于你的问题..这个prettyPhoto 是在哪里触发的(你说 afret 'a.fullSize' 被点击了,但这调用了`setTimeout(scrollSidebar, 1000);` 什么是your function - scrollSidebar()??(请原谅我太固执了)。最后,你为什么需要超时事件?只需调用函数本身。
    • 正如我所说,我关注了关于重新加载“代码块”的线程。我以前从来不需要重新加载 jQuery 函数,所以我做了那个线程上的回答。这就是问题所在:bit.ly/he94BR(如果滚动不起作用,请刷新站点)。现在点击图片用prettyPhoto打开它;您会注意到滚动功能已停止工作。那是我的问题。谢谢大佬。
    • @cr0z3r 首先,您所关注的问题是I need to reload a block of javascript every amount of time.so 每次特定时间重新加载一个函数(循环)。但不管。同样,您需要的只是再次调用您的函数。现在,当您给出代码示例时,它更容易理解。我已经更新了我的答案和工作代码。显然 PrettyImage 有一个 callbakc 功能(只是你需要的东西。甚至不点击,但是在关闭图像后,​​因为 PrettyyImage 搞砸了你的功能)。如果它的解决方案有效,请将答案标记为正确和 upovte! :)。谢谢人
    • 嘿伙计,非常感谢!到目前为止,这很好用。但是,在我触发 prettyPhoto 之前,滚动功能不起作用。它仅在 pretytPhoto 被触发后才起作用。我也尝试将函数 sideBar 放在 (document).ready() 中,但这并没有改变任何东西。
    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多