HTML部分代码
Start with having a wrapping container div called main_view , and two sections nested inside called image_reeland paging. The image_reel will contain the sliding images, and paging contains the paging controls. Take a look at the image below for a visual.
- <div class="main_view">
- <div class="window">
- <div class="image_reel">
- <a href="#"><img src="reel_1.jpg" alt="" /></a>
- <a href="#"><img src="reel_2.jpg" alt="" /></a>
- <a href="#"><img src="reel_3.jpg" alt="" /></a>
- <a href="#"><img src="reel_4.jpg" alt="" /></a>
- </div>
- </div>
- <div class="paging">
- <a href="#" rel="1">1</a>
- <a href="#" rel="2">2</a>
- <a href="#" rel="3">3</a>
- <a href="#" rel="4">4</a>
- </div>
- </div>
CSS部分代码
Take a look at the comments below for an explanation of the styles.
- /*--Main Container--*/
- .main_view {
- float: left;
- position: relative;
- }
- /*--Window/Masking Styles--*/
- .window {
- height:286px; width: 790px;
- overflow: hidden; /*--Hides anything outside of the set width/height--*/
- position: relative;
- }
- .image_reel {
- position: absolute;
- top: 0; left: 0;
- }
- .image_reel img {float: left;}
- /*--Paging Styles--*/
- .paging {
- position: absolute;
- bottom: 40px; right: -7px;
- width: 178px; height:47px;
- z-index: 100; /*--Assures the paging stays on the top layer--*/
- text-align: center;
- line-height: 40px;
- background: url(paging_bg2.png) no-repeat;
- display: none; /*--Hidden by default, will be later shown with jQuery--*/
- }
- .paging a {
- padding: 5px;
- text-decoration: none;
- color: #fff;
- }
- .paging a.active {
- font-weight: bold;
- background: #920000;
- border: 1px solid #610000;
- -moz-border-radius: 3px;
- -khtml-border-radius: 3px;
- -webkit-border-radius: 3px;
- }
- .paging a:hover {font-weight: bold;}
JS部分代码
The following script contains comments explaining which jQuery actions are being performed.
1.Setting up the Image Slider
Start by showing the paging and activating the first link. Then we will calculate and adjust the width of theimage_reel according to how many slides there are.
- //Show the paging and activate its first link
- //显示分页和激活其第一环节
- $(".paging").show();
- $(".paging a:first").addClass("active");
- //Get size of the image, how many images there are, then determin the size of the image reel.
- //获得图像的大小,有多少图片,然后确定图像的大小卷。
- var imageWidth = $(".window").width();
- var imageSum = $(".image_reel img").size();
- var imageReelWidth = imageWidth * imageSum;
- //Adjust the image reel to its new size
- //调整图像的卷轴,新的尺寸
- $(".image_reel").css({'width' : imageReelWidth});
2.Setting up the Slider Function and Timer
We first create the function for the slide event by itself (rotate ). Then create another function (rotateSwitch ) that will rotate and repeat that slide event (rotate).
- //Paging and Slider Function
- //页面和滑块函数
- rotate = function(){
- //Get number of times to slide
- //获取有多少次滑动
- var triggerID = $active.attr("rel") - 1;
- //Determines the distance the image reel needs to slide
- //确定幻片片卷动的距离
- var image_reelPosition = triggerID * imageWidth;
- //Remove all active class
- //移除active类
- $(".paging a").removeClass('active');
- //Add active class (the $active is declared in the rotateSwitch function)
- //添加active类(...)
- $active.addClass('active');
- //Slider Animation
- //滑动动画
- $(".image_reel").animate({
- left: -image_reelPosition
- }, 500 );
- };
- //Rotation and Timing Event
- //轮转和定时事件
- rotateSwitch = function(){
- play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
- //设置定时器-每7秒重复
- //Move to the next paging
- //移动到下一页
- $active = $('.paging a.active').next();
- //If paging reaches the end...
- //如果这是最后一页
- if ( $active.length === 0) {
- //go back to first
- //返回到第一页
- $active = $('.paging a:first');
- }
- //Trigger the paging and slider function
- //运行函数
- rotate();
- //Timer speed in milliseconds (7 seconds)
- //计时器的速度以毫秒计(7秒)
- }, 7000);
- };
- //Run function on launch
- //运行函数
- rotateSwitch();
Take a look at this tutorial for an explanation of how the timer (setInterval ) works.
3.Hover and Click Events
In case the user wants to view the slide for a longer period of time, we will allow the slider to stop when it is hovered. Another thing to consider is we should reset the timer each time the paging is clicked. This will prevent unexpected slide switches and allow for a smoother experience.
- //On Hover
- $(".image_reel a").hover(function() {
- clearInterval(play); //Stop the rotation
- }, function() {
- rotateSwitch(); //Resume rotation timer
- });
- //On Click
- $(".paging a").click(function() {
- $active = $(this); //Activate the clicked paging
- //Reset Timer
- clearInterval(play); //Stop the rotation
- rotateSwitch(); // Resume rotation timer
- rotate(); //Trigger rotation immediately
- return false; //Prevent browser jump to link anchor
- });
淡入淡出幻灯片效果
首先我们建一个DIV,里面包括5张img,其中主要css部分代码如下:
- #slider1{
- margin:20px auto;
- height:240px;
- width:740px;
- position:relative;
- }
- #slider1 img{
- position: absolute;
- top: 0px;
- left: 0px;
- display:none;
- }
原理分析:通过间隔一定时间来改变下一张图片的z-index,实现淡入淡出的幻灯片效果,具体js部分代码如下:
- var now=0;
- setInterval(function (){
- pre=now===0?2:now-1;
- nxt=now===4?0:now+1;
- var div=$("#slider1").children();
- div.eq(now).fadeOut(0,function(){
- div.css('z-index',1);
- div.eq(nxt).css("z-index",6).fadeIn(600);
- div.eq(pre).css("z-index",4);
- div.eq(now).css("z-index",5);
- now=nxt;
- });
- },3000);
滚动幻灯片效果
建立两个DIV,ID分别为slider2跟children,slider2为父div,children为子DIV,包含5张img,父 DIV(slider2)设置为overflow:hidden。主要CSS部分如下:
- #slider2{
- overflow:hidden;
- margin:20px auto;
- height:240px;
- width:740px;
- position:relative;
- }
- #children img{
- width:740px;
- height:240px;
- margin:0;
- padding:0;
- float:left;
- }
- #children{
- height:240px;
- position:relative;
- width:740px;
- }
原理分析:通过间隔一定时间,来改变图片的绝对位置,时间滚动动画的幻灯片代码,具体js部分代码如下:
- var slider=1;
- setInterval(function(){
- slider=slider===5?0:slider;
- var t=-slider*240;
- slider++;
- $("#children").animate({top:t},600);
- },3000);