javascript - Implement autoplay, pause on hover, keyboard control on slideshow -
i trying implement this sideshow on project, , want add features :
- auto-play
- pause on hover
- multiple instance of slider
- go next slide keyboard arrow keys
what have tried ?
i have added code
var autoscroll = window.setinterval(function(){ navigate( 'next' )}, 5000); component.onmouseover = function(e){ autoscroll = clearinterval(autoscroll); console.log('onmouseover'); }; component.onmouseout = function(e) { clearinterval(autoscroll); var autoscroll = setinterval(function() {navigate( 'next' )}, 5000); console.log('onmouseout'); };
live demo: http://jsfiddle.net/m2cwf/1/
what problem ?
the autoplay works fine until manually use control arrow, see interval change in case, maybe need add condition
if( isanimating ) return false;
the pause on hover not seems work, , console log see event fired several times in 1 single hover !
if have multiple instance of slider first 1 works
the keyboard keys pass next slide
thank you help.
the problem how handling mouseover , mouseout redefining global autoscroll variable instead of changing value in mouseout event.
changing , relatively simple keydown event code fixed slideshow want (i think).
the changes made mouseover/mouseout code:
var autoscroll = window.setinterval(function(){ navigate( 'next' )}, 5000); component.onmouseover = function(e){ autoscroll = clearinterval(autoscroll); console.log('onmouseover'); }; component.onmouseout = function(e) { clearinterval(autoscroll); autoscroll = setinterval(function() {navigate( 'next' )}, 5000); console.log('onmouseout'); };
and here's code arrow key events:
document.onkeydown = function(event) { event = event || window.event; switch (event.keycode || event.which) { case 37: clearinterval(autoscroll); navigate( 'prev' ); autoscroll = setinterval(function() {navigate( 'next' )}, 5000); break; case 39: clearinterval(autoscroll); navigate( 'next' ); autoscroll = setinterval(function() {navigate( 'next' )}, 5000); break; } };
here's came with: http://jsfiddle.net/m2cwf/2/
update
here's version supports multiple slideshows per page. eliminated left , right arrow actions though because i'm not sure how want handle navigating multiple slideshows on 1 page...
it shouldn't hard modify code make happen though.
Comments
Post a Comment