PrettyPhoto 触发后重新加载 jQuery 函数

发布于 2024-11-03 02:11:29 字数 3341 浏览 5 评论 0原文

我希望标题是不言自明的。无论哪种方式,我都试图在触发另一个进程后重新加载(或重新读取)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);

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

谢谢!
克里斯

I hope the title is self-explanatory. Either way, I am trying to reload (or re-read) a function in jQuery, after another process has been triggered (in this case, the prettyPhoto plug-in).

This is my current jQuery function:

        /** 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 } ?>

And prettyPhoto gets triggered when an anchor tag with the class fullSize is clicked.

I followed this thread to reload a 'code block', using the setTimeout method. This is what I did: (wrapped the previous code within a function, used setTimeout inside of that function and then called the setTimeout outside of the function)

    /** 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);

Sadly that did not work. So, do you have any ideas as to how I could reload/re-read a function once another action/plug-in has been triggered?

Thanks!
Chris

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

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

发布评论

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

评论(1

动次打次papapa 2024-11-10 02:11:30

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

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

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

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

$("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/

" I am trying to reload (or re-read) a function in jQuery, after another process has been triggered"

why are you calling setTimeout? this will trigger the function call after certain amount of time and then do loop/repeat every 1000ms. If you just want to reload the function, just call function itself.

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

EDIT:
Your pretty Image probably does something that affects the Scrolbar. However, http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/documentation/ has callback attribute. So do ii like this (callback function will be called whenever a perryimaege has been closed as the problem you were having is when it prettyimage gets open. Soin that respect taht's why click event didn't solve your problem (code probably was working)) You needed something AFTEr the image is closed

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

and declare your sidebar on other place

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'
                });
            }
        });
    }   
}

working code from your webiste http://jsfiddle.net/qeDwM/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文