自动手风琴
我正在尝试使手风琴菜单自动循环显示,而我不断发现的所有内容对于说明都非常模糊。我找到了几个答案,说我可以使用循环插件来循环,所以这就是我所尝试的。但我无法让循环功能正常工作。我的处理方式是否正确?如果是,我的代码中遗漏了什么?
我对 jQuery 很陌生,所以这一切对我来说有点不知所措。谢谢。
<script type="text/javascript" >
$(document).ready(function(){
lastBlock = $("#a1");
maxWidth = 380;
minWidth = 50;
$("#slide ul li a").hover(
function(){
$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
lastBlock = this;
}
);
});
$(function() {
$('#slide').cycle({
fx: 'slideY',
easing: 'bounceout',
delay: -4000
});
});
</script>
<div id="slide" class="cycle">
<ul>
<li>
<a id="a1">
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
</ul>
</div>
I am trying to make an accordion menu automatically cycle through, and every thing I keep finding is very vague about instructions. I have found a couple of answers that said I could use the cycle plugin to cycle through, so that's it what I've attempted. I'm not able to get the cycle function working properly though. Am I going about it the right way, and if so what have I missed in my code?
I am very new at jQuery, so all of this is a little overwhelming for me. Thanks.
<script type="text/javascript" >
$(document).ready(function(){
lastBlock = $("#a1");
maxWidth = 380;
minWidth = 50;
$("#slide ul li a").hover(
function(){
$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
lastBlock = this;
}
);
});
$(function() {
$('#slide').cycle({
fx: 'slideY',
easing: 'bounceout',
delay: -4000
});
});
</script>
<div id="slide" class="cycle">
<ul>
<li>
<a id="a1">
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
<li>
<a>
<img src="image" />
</a>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我不确定这是否正是您正在寻找的,但我编写了一个快速解决方案,希望可以帮助您找到解决方案:
HTML
jQuery
所以在这个解决方案是设置一个时间间隔来循环触发
mouseenter()
和mouseleave()
事件。由于您可能不希望鼠标悬停在某个元素上时图像循环,因此在mouseenter()
上清除间隔,并在mouseleave()
期间再次设置。一个可以重构的令人困惑的部分是它使用带有模数运算的计数器循环图像的方式。请注意,我向每个添加了一个 ID,并对末尾的数字进行了增量更改。
在这里查看它的工作原理: http://jsfiddle.net/Ck3aZ/
So I'm not sure if this is exactly what you are looking for, but I wrote a quick solution to hopefully help you get to your solution:
HTML
jQuery
So in this solution, an interval is set to cycle through and trigger the events
mouseenter()
andmouseleave()
. Since you might not want the images cycling while your mouse is over an element the interval is cleared uponmouseenter()
and set up again duringmouseleave()
. A confusing part that could be refactored is the way it cycles through the images using a counter with a modulus operation. Note that I added an ID to each<a>
with an incremental change to the number at the end.See it working here: http://jsfiddle.net/Ck3aZ/