jQuery:滑动切换 ||代码有什么优化吗?
大家好,我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“优化”这一点的唯一方法是使用回调而不是手动延迟函数。
.slideUp()
和.slideDown()
接受动画完成后执行的回调。使用链接是最佳实践,因此您不必重新创建对象(请参阅回调函数)。
此外,我还使用 jQuery 1.7 中添加的新
on()
更改了bind()
函数。如果你使用的是 jQuery < 1.7,使用
.click()
,它是.bind()
的简写。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 newon()
, which was added in jQuery 1.7.If you're on jQuery < 1.7, use
.click()
, which is a shorthand for.bind()
.在这里(滚动): http://jsfiddle.net/zWnLv/43/
到目前为止作为“点击阅读更多”,你有很多选择......你可以每次切换文本,或者,我的偏好,切换带有+/-(或箭头)背景图像的类,让用户直观地知道他们可以打开或关闭该部分。
Here you go (with scroll): http://jsfiddle.net/zWnLv/43/
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.