使用 jQuery 实现幻灯片和选项卡的最佳方法
我正在创建一个页面,顶部有图像,下面有菜单。当用户单击 3 个菜单按钮中的一个时,图像会向上滑动,页面会向下滚动,以便菜单位于页面顶部,然后右侧 .content div 会淡入。向上滑动应该仅在用户第一次使用时发生单击按钮。
使用 jQuery 执行此操作的绝对最佳方法是什么?(无插件)
我还需要知道如果我单击同一个按钮两次,如何无法阻止它在已经可见的页面中淡出?
我使用 rel 而不是 href,因为即使 return false,href 也会导致页面跳转。
这是我到目前为止所拥有的:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
imgVisible = true;
$('#mainmenu a').click(function(){
var $activeTab = $(this).attr('rel');
if(!imgVisible){
$('html:not(:animated),body:not(:animated)').animate({scrollTop:$('#mainmenu').offset().top-20},500);
$('.content').hide();
$($activeTab).fadeIn();
} else{
$('#imgholder').slideUp(500,function(){
imgVisible = false;
$('#mainmenu a[rel="'+$activeTab+'"]').click();
});
}
return false;
});
});
</script>
<div id="imgholder"><img src="image.jpg" /></div>
<div id="mainmenu">
<ul>
<li><a rel="#tab1"></a></li>
<li><a rel="#tab2"></a></li>
<li><a rel="#tab3"></a></li>
</ul>
</div>
<div id="container">
<div class="content" id="tab1">
content
</div>
<div class="content" id="tab2">
content
</div>
<div class="content" id="tab3">
content
</div>
</div>
I'm creating a page with an image at the top, and a menu below. When the user clicks on on of the 3 menu buttons, the image slideUp and the page scrolls down so the menu is at the top of the page, then the right .content div fades in. The slideUp should only happen the first time the user clicks on of the buttons.
What the absolute best way to do this with jQuery? (no plugins)
I also need to know how I can't prevent it to fade in the page that is already visible if i click the same button twice?
I'm using rel instead of href, since the href made the page jump, even with return false.
This is what I have so far:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
imgVisible = true;
$('#mainmenu a').click(function(){
var $activeTab = $(this).attr('rel');
if(!imgVisible){
$('html:not(:animated),body:not(:animated)').animate({scrollTop:$('#mainmenu').offset().top-20},500);
$('.content').hide();
$($activeTab).fadeIn();
} else{
$('#imgholder').slideUp(500,function(){
imgVisible = false;
$('#mainmenu a[rel="'+$activeTab+'"]').click();
});
}
return false;
});
});
</script>
<div id="imgholder"><img src="image.jpg" /></div>
<div id="mainmenu">
<ul>
<li><a rel="#tab1"></a></li>
<li><a rel="#tab2"></a></li>
<li><a rel="#tab3"></a></li>
</ul>
</div>
<div id="container">
<div class="content" id="tab1">
content
</div>
<div class="content" id="tab2">
content
</div>
<div class="content" id="tab3">
content
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码完成您所需要的:
我已从您的 rel='' 片段中删除了“#”符号;-)
我不确定您为什么要滚动页面。当用户单击菜单时,他/她已经将其聚焦(因此它在当前视口内可见)。但是你有一个非常大的顶部图像吗?如果是这种情况,请告诉我,我将修改该代码片段。 (不过,这取决于页面首次加载时菜单下方可见的内容量。)
此外,对于 SEO 是您可能希望使用 href 而不是 rel 属性并创建单独的内容保存页面的原因。以下代码片段将删除导航操作。
The following code accomplishes what you need:
I have removed the '#' sign from your rel='' piece ;-)
I am not sure why you would want to scroll the page. When a user clicks on the menu, he/she already has it focused (so it is visible inside the current viewport). But do you have a very large top image? If that is the case, let me know and I will modify the snippet. (Still, it depends on the amount of content below the menu visible when the page first loads.)
Also, for SEO reasons you might want to use the href instead of the rel attribute and create separate content holding pages. The following snippet would remove the navigation action.
我认为这是您寻找的一个很好的例子:有机标签
I think this is a great example of what your looking for: Organic Tabs
我强烈建议添加
因为正确完成后不会使页面跳转,并且会确保对锚链接进行验证。如果我错过了什么,有人让我知道,它可以很快解决(或者如果您有优化或改进,您可以为我做)。
I highly suggest adding
<a href="#">
as this will not make the page jump when done properly and will ensure validation on your anchor links. Someone let me know if I missed something, it can be resolved quickly (or you can do it for me if you have an optimization or improvement).