简体   繁体   English

使用Jquery,javascript和CSS自动播放图像滑块

[英]Autoplay image slider with Jquery,javascript and css

i have created this slider with jQuery and javascript but I can't make this work with the outplay and with next-previous button. 我已经使用jQuery和javascript创建了此滑块,但是我无法通过outout和下一个上一个按钮来实现此功能。 I need this slider for image as title said, could you help me with left/right side animation? 如标题所述,我需要此滑块来显示图像,您能帮我左右动画吗?

$(document).ready(function(){
$('.sp').first().addClass('active');
$('.sp').hide();    
$('.active').show();

$('.active').removeClass('active').addClass('oldActive');    
        if ( $('.oldActive').is(':last-child')) 
        {
            $('.sp').first().addClass('active');
        }
        else
        {
            $('.oldActive').next().addClass('active');
        }
    $('.oldActive').removeClass('oldActive');
    setTimeout('.active', 1000);
    $('.sp').fadeOut();
    $('.active').fadeIn();

the other part of the code is in jsfiddle link this is the code https://jsfiddle.net/ghy14p0f/1/ 该代码的另一部分在jsfiddle链接中,这是代码https://jsfiddle.net/ghy14p0f/1/

Easiest way to do the slide is to include the jquery-ui module, to allow for effects and easing. 制作幻灯片的最简单方法是包括jquery-ui模块,以实现效果和缓和。 I've added it below, and created the transitions. 我在下面添加了它,并创建了转换。 Also, in your fiddle, you never actually tell it to use jquery -- so yeah, it would never work. 另外,在您的小提琴中,您实际上从未告诉过它使用jquery -是的,它永远都行不通。 I didn't actually change much, the only lines I edited were the fadeout/fadein to make them transitions. 我实际上并没有太大的改变,我编辑的唯一行是淡入淡出/淡入淡出以使其过渡。 Best of luck!! 祝你好运!

 $(document).ready(function() { $('.sp').first().addClass('active'); $('.sp').hide(); $('.active').show(); $('#button-next').click(function() { $('.active') .removeClass('active') .addClass('oldActive'); if ($('.oldActive').is(':last-child')) { $('.sp').first().addClass('active'); } else { $('.oldActive').next().addClass('active'); } $('.oldActive').hide("slide", { direction: "right" }, 600).removeClass('oldActive'); $('.active').delay(400).show("slide", { direction: "left" }, 600); }); $('#button-previous').click(function() { $('.active').removeClass('active').addClass('oldActive'); if ($('.oldActive').is(':first-child')) { $('.sp').last().addClass('active'); } else { $('.oldActive').prev().addClass('active'); } $('.oldActive').hide("slide", { direction: "left" }, 600).removeClass('oldActive'); $('.active').delay(400).show("slide", { direction: "right" }, 600); }); }); 
 #slider-wrapper { width: 500px; height: 200px; } #slider { width: 500px; height: 200px; position: relative; } .sp { width: 500px; height: 200px; position: absolute; } img { height: 200px; } #nav { margin-top: 20px; width: 100%; } #button-previous { border: 1px dotted blue; background-color: #ccc; float: left; } #button-next { border: 1px dotted blue; background-color: #ccc; float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <div id="slider-wrapper"> <div id="slider"> <div class="sp"> <img src="https://upload.wikimedia.org/wikipedia/commons/3/32/Bianco_e_Rosso_(Croce)_e_Rosso.png"> First Image </div> <div class="sp"> <img src="https://upload.wikimedia.org/wikipedia/commons/f/f0/600px_Rosso_e_Giallo.PNG"> Second Image </div> <div class="sp"> <img src="https://upload.wikimedia.org/wikipedia/en/4/4c/Flag_of_Sweden.svg"> Third Image </div> </div> </div> <div id="nav"></div> <div id="button-previous">prev</div> <div id="button-next">next</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM