How can you change effect in Nivo Slider based off of previous or next slide keypress?

喜欢而已 提交于 2019-12-01 01:48:31
$('.nivo-prevNav').live('mouseover', function(){
     $('#slider img').attr("data-transition","sliceUpDown");
});

$('.nivo-nextNav').live('mouseover', function(){
     $('#slider img').attr("data-transition","sliceUpDownLeft");
});

Per the nivo slider documentation here you can change the effect for each slide by adding a custom data attribute onto any or all of the images and it will override nivo's default transition:

<img src="images/slide1.jpg" alt="" data-transition="slideInLeft" />

You can alter this custom data dynamically by setting an event handler on the document body to look for a keypress and then change the attr of all the images using the snippet below:

     $('body').on('keypress', function(){
            $('img').attr("data-transition","slideInLeft");
            });

You can get more advanced and change to specific types of transitions based on the key pressed by adding some logic in, but this is the general idea. A working fiddle is here: http://jsfiddle.net/gregjuva/2ZXCp/

Add this to "jquery.nivo.slider.js" before comment "// Run effects" after comment and code in "// Custom transition as defined by "data-transition" attribute". This show change current effect if you click on left or right arrow or buttons. For this work you must have imageis in HTML without "data-transition" attribute and default effect you must define in "jquery.nivo.slider.js" under comment "//Default settings" because "data-transition" attribute is prefer. I code it right know for my project.

        if(nudge === 'prev'){
            currentEffect = 'slideInLeft';
        } 
        else if (nudge === 'next'){
            currentEffect = 'slideInRight';
        }
        else if (nudge === 'control'){
            currentEffect = 'fade'; /*test*/
        }

Using Button

<script type='text/javascript'>
$(document).ready(function() {
    jQuery("#previousButton').click(function (e) {
         e.preventDefault();
         jQuery(".nivo-directionNav .nivo-prevNav").click();
    });
    jQuery("#nextButton').click(function (e) {
         e.preventDefault();
         jQuery(".nivo-directionNav .nivo-nextNav").click();
    });
});
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!