jQuery:滑动切换 ||代码有什么优化吗?

发布于 2024-12-21 14:26:26 字数 1755 浏览 0 评论 0原文

大家好,我是 jQuery 编程新手,我有一个快速代码片段,我希望有人为我测试一下。

我认为它运作良好..但我不知道这是否是处理它的最佳方法。

工作示例: http://jsfiddle.net/zWnLv/29/

//hide wrapper at document ready
        $('#newsbox_content_wrapper').hide();

        //toggle visiblility of newsbox and slide down to scroll window to newsbox
        $('.newsbox_toggle').bind('click', function () {
            //define speed for effect
            var $speed = 400;

            //check to see if the class 'open' exists then run
            if ($('.newsbox_toggle').hasClass('open')) {
                //scroll to the top of the newsbox
                $('html, body').animate({scrollTop: $('#header_lower').offset().top}, $speed);
                $('#newsbox_content_wrapper').slideDown($speed);
                $('.newsbox_toggle').removeClass('open');
                //delay HTML replacement to sync with animation
                $('.newsbox_expand').delay($speed).queue(function(n) {
                    $(this).html('Click to Close News Feature.');
                    $(this).addClass('rotate');
                    n();
                });
            } else {
                //scroll back to top of body
                $('html, body').animate({ scrollTop: 0 }, $speed);
                $('#newsbox_content_wrapper').slideUp($speed);
                $('.newsbox_toggle').addClass('open');
                //delay HTML replacement to sync with animation
                $('.newsbox_expand').delay($speed).queue(function(n) {
                     $(this).html('Click to Read More.');
                     $(this).removeClass('rotate');
                     n();
                });
            }
        });

Hi Im new to programming jQuery and i have a quick code snippet that I would love someone to test for me.

I think it works well.. but i dont know if its the best way to handle it.

working example: http://jsfiddle.net/zWnLv/29/

//hide wrapper at document ready
        $('#newsbox_content_wrapper').hide();

        //toggle visiblility of newsbox and slide down to scroll window to newsbox
        $('.newsbox_toggle').bind('click', function () {
            //define speed for effect
            var $speed = 400;

            //check to see if the class 'open' exists then run
            if ($('.newsbox_toggle').hasClass('open')) {
                //scroll to the top of the newsbox
                $('html, body').animate({scrollTop: $('#header_lower').offset().top}, $speed);
                $('#newsbox_content_wrapper').slideDown($speed);
                $('.newsbox_toggle').removeClass('open');
                //delay HTML replacement to sync with animation
                $('.newsbox_expand').delay($speed).queue(function(n) {
                    $(this).html('Click to Close News Feature.');
                    $(this).addClass('rotate');
                    n();
                });
            } else {
                //scroll back to top of body
                $('html, body').animate({ scrollTop: 0 }, $speed);
                $('#newsbox_content_wrapper').slideUp($speed);
                $('.newsbox_toggle').addClass('open');
                //delay HTML replacement to sync with animation
                $('.newsbox_expand').delay($speed).queue(function(n) {
                     $(this).html('Click to Read More.');
                     $(this).removeClass('rotate');
                     n();
                });
            }
        });

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

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

发布评论

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

评论(2

吃不饱 2024-12-28 14:26:26

“优化”这一点的唯一方法是使用回调而不是手动延迟函数。 .slideUp().slideDown() 接受动画完成后执行的回调。
使用链接是最佳实践,因此您不必重新创建对象(请参阅回调函数)。

此外,我还使用 jQuery 1.7 中添加的新 on() 更改了 bind() 函数。

$('.newsbox_toggle').on('click', function () {
    //define speed for effect
    var $speed = 400;

    //check to see if the class 'open' exists then run
    if ($('.newsbox_toggle').hasClass('open')) {
        //scroll to the top of the newsbox
        $('html, body').animate({scrollTop: $('#header_lower').offset().top}, $speed);
        $('#newsbox_content_wrapper').slideDown($speed, function() {
            $('.newsbox_expand').html('Click to Close News Feature.').addClass('rotate');
        });
        $('.newsbox_toggle').removeClass('open');
    } else {
        //scroll back to top of body
        $('html, body').animate({ scrollTop: 0 }, $speed);
        $('#newsbox_content_wrapper').slideUp($speed, function() {
            $('.newsbox_expand').html('Click to Read More.').removeClass('rotate');
        });
        $('.newsbox_toggle').addClass('open');
    }
});

如果你使用的是 jQuery < 1.7,使用.click(),它是.bind()的简写。

$('.newsbox_toggle').click(function () {
    // ...
});

The only way you could "optimize" this is using callbacks instead of manually delaying functions. .slideUp() and .slideDown() accept callbacks to be executed after the animation finishes.
Using chaining is a best practice, so you don't have to recreate objects (see the callback functions).

Also, I've changed the bind() function with the new on(), which was added in jQuery 1.7.

$('.newsbox_toggle').on('click', function () {
    //define speed for effect
    var $speed = 400;

    //check to see if the class 'open' exists then run
    if ($('.newsbox_toggle').hasClass('open')) {
        //scroll to the top of the newsbox
        $('html, body').animate({scrollTop: $('#header_lower').offset().top}, $speed);
        $('#newsbox_content_wrapper').slideDown($speed, function() {
            $('.newsbox_expand').html('Click to Close News Feature.').addClass('rotate');
        });
        $('.newsbox_toggle').removeClass('open');
    } else {
        //scroll back to top of body
        $('html, body').animate({ scrollTop: 0 }, $speed);
        $('#newsbox_content_wrapper').slideUp($speed, function() {
            $('.newsbox_expand').html('Click to Read More.').removeClass('rotate');
        });
        $('.newsbox_toggle').addClass('open');
    }
});

If you're on jQuery < 1.7, use .click(), which is a shorthand for .bind().

$('.newsbox_toggle').click(function () {
    // ...
});
錯遇了你 2024-12-28 14:26:26

在这里(滚动): http://jsfiddle.net/zWnLv/43/

//hide wrapper at document ready and put in var for re-use
var newsbox = $('#newsbox_content_wrapper').hide();

//toggle visiblility of newsbox and slide down to scroll window to newsbox
$('.newsbox_toggle').bind('click', function() {   
    newsbox.slideToggle("slow",function() {
        $('html, body').animate({ scrollTop: newsbox.offset().top }, 'slow');
    });

});

到目前为止作为“点击阅读更多”,你有很多选择......你可以每次切换文本,或者,我的偏好,切换带有+/-(或箭头)背景图像的类,让用户直观地知道他们可以打开或关闭该部分。

Here you go (with scroll): http://jsfiddle.net/zWnLv/43/

//hide wrapper at document ready and put in var for re-use
var newsbox = $('#newsbox_content_wrapper').hide();

//toggle visiblility of newsbox and slide down to scroll window to newsbox
$('.newsbox_toggle').bind('click', function() {   
    newsbox.slideToggle("slow",function() {
        $('html, body').animate({ scrollTop: newsbox.offset().top }, 'slow');
    });

});

As far as the "click to read more" you have plenty of options... you could toggle the text each time or, my preference, toggle a class with a +/- (or arrows) background image to let a user intuitively know they can open or close that section.

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