简单的 jQuery Animate 示例 - 从一个 div 滚动到另一个 div

发布于 2024-11-05 20:18:27 字数 385 浏览 1 评论 0原文

这是我要创建的示例...

假设有两个 div,每个 div 都包含一些其他 HTML/内容(其他 div)。我希望在页面加载时在视图中显示其中一个 div,然后在几秒后(假设是 5 秒),将第二个 div 滚动到与第一个 div 相同的位置,然后无限期地重复该过程,直到用户离开页面。

相关页面和元素可以在 http://paysonfirst assembly.com/ 中找到。我正在尝试使用一类dynamicPanel 为左侧边栏设置动画。至少有三个这样的 div,它们的内容长度几乎匹配。

我感谢大家的帮助。我是一名非常新的客户端程序员,并且很欣赏这个社区对新开发人员的尊重。

Here is the example I'm looking to create...

Let's assume there are two divs, each containing some other HTML/Content (other divs). I would like to have one of these divs in the view on page load, and then after some number of seconds (let's say 5), scroll the second div onto the same place as the first div, and then repeating that process indefinitely until the user leaves the page.

The page and elements in question can be found at http://paysonfirstassembly.com/. I am attempting to animate the left sidebar with a class of dynamicPanel. There will be at least three of these divs, and they will nearly match up in content length.

I appreciate everybody's help. I am a very new client-side programmer and appreciate the respect that this community has with new developers.

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

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

发布评论

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

评论(1

同展鸳鸯锦 2024-11-12 20:18:27

以下内容的工作演示 →

这是我刚刚制作的一个简单的 jQuery 插件将向上滑动第一个 div 并将其放置在列表的末尾。我对下面的代码进行了注释以进一步解释,以便您可以开始使用,您可以根据您的需要进行调整并了解 jQuery:

// the plugin declaration
$.fn.rotateEach = function ( opts ) {

    // cache the element set
    var $this = this,

        // create some default options
        defaults = {
            delay: 5000
        },

        // pass the defaults to settings with any override options
        settings = $.extend(defaults, opts),

        // repeated rotation function
        rotator = function ( $elems ) {

            // slide up first element in set
            $elems.eq(0).slideUp(500, function(){

                // detach first element
                var $eq0 = $elems.eq(0).detach();

                // append it to wrapper
                $elems.parent().append($eq0);

                // fade it back in
                $eq0.fadeIn();

                // call rotator on reselection of elements
                // since first element was moved to end
                setTimeout(function(){ rotator( $( $elems.selector ) ); },
                           settings.delay);
            });
        };

    // initial rotator call
    setTimeout(function(){ rotator( $this ); }, settings.delay);
};

// invoke plugin
$('.dynPanelContent').rotateEach();

如果您想更改延迟,您可以将其作为选项传递:

$('.dynPanelContent').rotateEach({ delay: 7500 }); // 7.5 seconds

注意:我还将 .dynPanelOpener.dynPanelTitle 移至 .dynPanelContent 中,以便将它们包含在动画中。

查看工作示例→

Working demo of the following →

Here's a simple jQuery plugin I just made that will slide up the first div and place it at the end of the list. I've commented the code below to further explain so that this can just get you started and you can adjust it to your needs and learn about jQuery:

// the plugin declaration
$.fn.rotateEach = function ( opts ) {

    // cache the element set
    var $this = this,

        // create some default options
        defaults = {
            delay: 5000
        },

        // pass the defaults to settings with any override options
        settings = $.extend(defaults, opts),

        // repeated rotation function
        rotator = function ( $elems ) {

            // slide up first element in set
            $elems.eq(0).slideUp(500, function(){

                // detach first element
                var $eq0 = $elems.eq(0).detach();

                // append it to wrapper
                $elems.parent().append($eq0);

                // fade it back in
                $eq0.fadeIn();

                // call rotator on reselection of elements
                // since first element was moved to end
                setTimeout(function(){ rotator( $( $elems.selector ) ); },
                           settings.delay);
            });
        };

    // initial rotator call
    setTimeout(function(){ rotator( $this ); }, settings.delay);
};

// invoke plugin
$('.dynPanelContent').rotateEach();

If you want to change the delay you can just pass it in as an option:

$('.dynPanelContent').rotateEach({ delay: 7500 }); // 7.5 seconds

Note: I also moved .dynPanelOpener and .dynPanelTitle within .dynPanelContent so that they're included in the animations.

See working example →

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