javascript - Adding keyboard arrow navigation to custom js -


i using wordpress , content looks this

<div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/2/"><img src="blah1.jpg" alt=""/></a></div><!--nextpage--> <div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/3/"><img src="blahab.jpg" alt=""/></a></div><!--nextpage--> <div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/4/"><img src="blahco.jpg" alt=""/></a></div><!--nextpage--> <div class="image-wrap"><a class="ajax-load-next" href="http://linktopage.com/5/"><img src="blahneat.jpg" alt=""/></a></div> 

i have custom javascript loads next image when user clicks on image. want add left & right keyboard arrow navigation script , don't know how can implement since i'm not familiar javascript.

$('body').on('click', '.image-wrap', function(e) { // listen 'click' on our '.image-wrap' element   e.preventdefault();  // prevents default behavior on element    $.ajax({      url: $(this).find( 'a' ).attr( 'href' ), // url fetching ajax     success: function (response) {        newimg = $(response).find('.image-wrap').html(), // new href link , image response, contained in div.image-wrap        $( 'div.image-wrap' ).html( newimg ); // set new html our inner div      }   }).fail(function (data) {      if ( window.console && window.console.log ) {        console.log( data );  // log failure console      }    });  }); 

edit: pressing right arrow key want click ajax link inside image-wrap div should load next image. if pressing left arrow key should go previous image. idea how this?

you can use mousetrap.

function gotolocation(url)    {      window.location = url;    }    mousetrap.bind("right", function() {  document.getelementbyid('next-image').click();    });
<script src="https://craig.global.ssl.fastly.net/js/rainbow-custom.min.js?39e99"></script>  <script src="https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.js?bc893"></script>    <div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/2/"><img src="blah1.jpg" alt=""/></a></div><!--nextpage-->  <div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/3/"><img src="blahab.jpg" alt=""/></a></div><!--nextpage-->  <div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/4/"><img src="blahco.jpg" alt=""/></a></div><!--nextpage-->  <div class="image-wrap"><a id="next-image" class="ajax-load-next" href="http://linktopage.com/5/"><img src="blahneat.jpg" alt=""/></a></div>

if use attachment.php or image.php based gallery. can use : wordpress attachment page navigate keyboard


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -