简体   繁体   English

自定义JQuery滑块(自动滑动)

[英]Custom JQuery Slider (auto-slide)

I'm trying to make image slider with my own without using plugins. 我试图用我自己的图像滑块而不使用插件。

1st question : How to make the animation horizontally 第一个问题:如何水平制作动画

2nd question : Whatever the size of the image, it must cover the height and width of its container, but keeping the proportionalities original image. 第二个问题:无论图像大小如何,它都必须覆盖容器的高度和宽度,但要保持原始图像的比例。 The image can be rendered partially. 图像可以部分渲染。 How to make this? 怎么做?

3rd question : About the code of the slide, if anyone finds any improvement to make it lighter and more elegant, would be welcome. 第三个问题:关于幻灯片的代码,如果有人发现任何改进使其更轻巧,更美观,将不胜感激。

 $(function(){ setInterval(function(){ var displayed = $(".img-header.displayed"); displayed.animate({opacity : 0}, 500, function() { displayed.css("display","none"); displayed.addClass("not-displayed").removeClass("displayed"); if(displayed.next(".img-header.not-displayed").length == 0){ $(".img-header:first").css("display","inline-block").css("opacity","1"); $(".img-header:first").addClass("displayed").removeClass("not-displayed"); $(".img-header:first").animate({opacity : 1}, 500); } else{ displayed.next(".img-header.not-displayed").css("display","inline-block").css("opacity","1"); displayed.next(".img-header.not-displayed").addClass("displayed").removeClass("not-displayed"); displayed.next(".img-header.not-displayed").animate({opacity : 1}, 500); } }); }, 4000); }); 
 #slider-container{height: 200px; width: 200px;} #slider-container img.img-header{ width: 100%; height: auto;} .displayed{display: inline-block;} .not-displayed{display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="slider-container"> <img class="img-header displayed" src="http://i.stack.imgur.com/AIHnl.png" /> <img class="img-header not-displayed" src="http://i.stack.imgur.com/XQrms.png" /> </div> 

I think you might be looking for something like this. 我认为您可能正在寻找类似的东西。

The slider here is position:relative; 滑块在这里是position:relative; with top:100px; top:100px; you can set as per your requirement. 您可以根据需要进行设置。 Still i suggest you to keep it positioned relative. 我仍然建议您保持相对位置。

Slider has width:700px and height:500px; 滑块的width:700pxheight:500px; , you can change as per your requirement, it will be fine for whatever aspect ratio of image you have or all images with different aspect ratio. ,您可以根据需要进行更改,无论您拥有的图像的纵横比是多少,还是所有具有不同纵横比的图像都可以。

There is an array for images to load which are serially numbered in one location, so you may give little more understanding into that. 有一个要加载的图像array ,这些图像在一个位置被顺序编号,因此您可能对此没有更多的了解。 I have made a comment for that in JS file 我已经在JS file对此发表了评论

Also you could change slider speed and delay as per your requirements. 您也可以根据需要更改滑块速度和延迟。

When you hover over image it will pause the slider , which resumes after you leave image. 当您将hover在图片上时,它将暂停滑块 ,该滑块会在您离开图片后继续显示。

Snippet : 片段:

 $(document).ready(function(){ var noPannel = document.getElementsByClassName("pannel").length; var i; var imgArr = [ ]; var pannelWidth = $(".slider_holder").width(); var totalWidth = noPannel*pannelWidth; for (i=1;i<=noPannel;i++) { imgArr[i] = "http://www.picget.net/background/" + i + ".jpg"; //if you have somewhere on other path //imgArr[i] = " " + i + ".jpg"; //use this if you have image in same folder/path. } for(i=1;i<=noPannel;i++) { $(".pannel:nth-child("+i+")").css("background","url("+imgArr[i]+")"); } function jsslider() { var curScroll = $(".slider").scrollLeft(); var endScroll = totalWidth - (pannelWidth*2); if(curScroll<=endScroll) { $(".slider").animate({scrollLeft: '+=' + pannelWidth +'px'},900,"swing");// Replace 900 for speed } else { $(".slider").animate({scrollLeft: '0px'},500,"swing"); // Replace 500 for speed to go bck to first } } var interval = setInterval(jsslider, 3000); // Replace 3000 for delay between each slide. $(".pannel").hover(function () { clearInterval(interval); }, function () { interval = setInterval(jsslider, 3000); // Replace 3000 for delay between each slide. }); }); // document.ready ENDS 
 html, body, * { margin:0px; width:100%; height:100%; } .slider_holder { margin-left:auto; margin-right:auto; display:block; position:relative; top:100px; width:1024px; height:768px; background:#eee; overflow:hidden; } .slider { width:auto; height:100%; width:100%; white-space: nowrap; overflow:hidden; } .pannel { margin:0px; display:inline-block; width:100%; height:calc(100% - 1px); /*border:1px solid red;*/ background-size:cover !important; background-repeat:no-repeat; background-position:50% 50% !important; position:relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <body onload="sliderFunc()"> <div class="slider_holder"> <div class="slider"> <span class="pannel"> </span> <span class="pannel"> </span> <span class="pannel"> </span> <span class="pannel"> </span> <span class="pannel"> </span> </div> </div> </body> 

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

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