jquery - simple fade in and out of images with arrow left and right -
the current script works fine, need use arrow left go previous image , arrow right go next one.
<script type="text/javascript"> var $images; var gonext = function() { var $this = $(this), $next = $this.next('img').length > 0 ? $this.next('img') : $this.siblings('img').eq(0); $this.hide(); $next.show(); return false; }; $(document).ready(function(){ $images = $('.images img'); $images.eq(0).siblings().hide(); $images.on('click', gonext); }); </script>
how can use following control images sliding in , out?
$("body").keydown(function(e) { if(e.keycode == 37) { // left } });
if wanted use left arrow go need do?
if want go next when right
pressed, trigger click
event on images -
$("body").keydown(function (e) { if(e.keycode == 39) { // right $('.images img:visible').trigger('click'); } });
demo.
i believe can tweak both way navigation.
full code. markup -
<div class="images"> <img src="images/1.jpg" width="1920" alt="1"> <img src="images/2.jpg" width="1920" alt="2"> <img src="images/3.jpg" width="1920" alt="3"> <img src="images/4.jpg" width="1920" alt="4"> <img src="images/5.jpg" width="1920" alt="5"> <img src="images/6.jpg" width="1920" alt="6"> <img src="images/7.jpg" width="1920" alt="7"> <img src="images/8.jpg" width="1920" alt="8"> <img src="images/9.jpg" width="1920" alt="9"> <img src="images/10.jpg" width="1920" alt="10"> </div>
javascript -
$(document).ready(function () { var $images; var gonext = function (event, direction) { var $this = $(this), $next; if (direction == 'left') { $next = $this.prev('img').length > 0 ? $this.prev('img') : $this.siblings('img:last'); } else { $next = $this.next('img').length > 0 ? $this.next('img') : $this.siblings('img:first'); } $this.hide(); $next.show(); return false; }; $images = $('.images img'); $images.eq(0) .siblings() .hide(); $images.on('click', gonext); $("body").keydown(function(e) { if (e.keycode == 39) { // right $('.images img:visible').trigger('click', 'right'); } else if (e.keycode == 37) { // left $('.images img:visible').trigger('click', 'left'); } }); });
Comments
Post a Comment