admin 管理员组文章数量: 1086019
I am working to build a small jQuery-based carousel without a plugin and I just can't seem to make the animations work. This isn't supposed to auto-rotate, but instead only rotate when the user clicks 'next' or 'prev' buttons. The only method I can get to work is automatically removing the last element to replace it just before the first element... this happens within a split-second and while it is continuous, there is no animation at all.
Here's my HTML container:
<div class="carousel-nav" clearfix">
<img src="img/prev.png" id="prv-testimonial">
<img src="img/next.png" id="nxt-testimonial">
</div>
<div id="carousel-wrap">
<ul id="testimonial-list" class="clearfix">
<li>
<p class="context">Some testimonial goes right here[1]</p>
<span class="creds">Tiffany LastName</span>
</li>
<li>
<p class="context">"We could not be more pleased. A++ efforts!"</p>
<span class="creds">Alan Goodwrench</span>
</li>
<li>
<p class="context">"After just one purchase, we know this is the pany to stick with."</p>
<span class="creds">Butters Stotch</span>
</li>
</ul>
</div>
The #carousel-wrap behaves as the viewport fixed at 400px(each list item is also 400px). The #testimonial-list is dynamically resized in jQuery based on the total number of items... so 4 items at 400px ~= 1600px along with some margins/padding. This keeps all the testimonials next to each other so they should animate into view from the right/left side.
Here is my very basic jQuery which honestly isn't performing as well as I'd hoped:
$('#prv-testimonial').on('click', function(){
var lastitm = $('#testimonial-list li:last').remove();
$('#testimonial-list li:first').before(lastitm);
});
There are only 2 things I need to figure out:
When a user clicks next/prev buttons how do I trigger the animation from right-to-left or vice-versa?
Once the animation finishes, how do I ensure that the carousel will loop infinitely? How can I get the "prev" button to move from the 1st item all the way to the last item and still hold continuity?
Any advice would be more than appreciated!
I am working to build a small jQuery-based carousel without a plugin and I just can't seem to make the animations work. This isn't supposed to auto-rotate, but instead only rotate when the user clicks 'next' or 'prev' buttons. The only method I can get to work is automatically removing the last element to replace it just before the first element... this happens within a split-second and while it is continuous, there is no animation at all.
Here's my HTML container:
<div class="carousel-nav" clearfix">
<img src="img/prev.png" id="prv-testimonial">
<img src="img/next.png" id="nxt-testimonial">
</div>
<div id="carousel-wrap">
<ul id="testimonial-list" class="clearfix">
<li>
<p class="context">Some testimonial goes right here[1]</p>
<span class="creds">Tiffany LastName</span>
</li>
<li>
<p class="context">"We could not be more pleased. A++ efforts!"</p>
<span class="creds">Alan Goodwrench</span>
</li>
<li>
<p class="context">"After just one purchase, we know this is the pany to stick with."</p>
<span class="creds">Butters Stotch</span>
</li>
</ul>
</div>
The #carousel-wrap behaves as the viewport fixed at 400px(each list item is also 400px). The #testimonial-list is dynamically resized in jQuery based on the total number of items... so 4 items at 400px ~= 1600px along with some margins/padding. This keeps all the testimonials next to each other so they should animate into view from the right/left side.
Here is my very basic jQuery which honestly isn't performing as well as I'd hoped:
$('#prv-testimonial').on('click', function(){
var lastitm = $('#testimonial-list li:last').remove();
$('#testimonial-list li:first').before(lastitm);
});
There are only 2 things I need to figure out:
When a user clicks next/prev buttons how do I trigger the animation from right-to-left or vice-versa?
Once the animation finishes, how do I ensure that the carousel will loop infinitely? How can I get the "prev" button to move from the 1st item all the way to the last item and still hold continuity?
Any advice would be more than appreciated!
Share Improve this question asked Dec 22, 2013 at 21:05 JakeJake 1,29511 gold badges40 silver badges121 bronze badges1 Answer
Reset to default 5Here's a try:
$('#prv-testimonial').on('click', function(){
var $last = $('#testimonial-list li:last');
$last.remove().css({ 'margin-left': '-400px' });
$('#testimonial-list li:first').before($last);
$last.animate({ 'margin-left': '0px' }, 4000); });
$('#nxt-testimonial').on('click', function(){
var $first = $('#testimonial-list li:first');
$first.animate({ 'margin-left': '-400px' }, 4000, function() {
$first.remove().css({ 'margin-left': '0px' });
$('#testimonial-list li:last').after($first);
});
});
I'm using the jquery .animate()
function to animate the "margin-left" css style. The Next button animates and then moves the first element to the end of the list. The Previous button moves the last item to the front of the list and then animates.
BTW: The value "4000" is the duration of the animation in milliseconds.
jsfiddle
本文标签: javascriptHow To Custom Animate ltulgt Carousel Slider with jQueryStack Overflow
版权声明:本文标题:javascript - How To Custom Animate <ul> Carousel Slider with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744096224a2532945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论