首页 > 代码库 > JQuery学习(2)
JQuery学习(2)
实现图片列表的自动滚动,单击左右导航的图片移动,以及单击图片放大的功能:
很多代码细节跟HTML、CSS相关,But······
$(document).ready(function() { /* * this function will make the carousel scroll auto-matically. * it is no different than the scrollRight click function * aside from missing a binding to a click function. */ function autoCarousel() { //outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距),li的宽度设置的160px,itemWidth返回170px; var itemWidth = $('#carouselUL li').outerWidth() + 10; //left设置的-340px; 返回的moveFactor为-510px; var moveFactor = parseInt($('#carouselUL').css('left')) - itemWidth; /* animate the carousel */ /* $(selector).animate({params},speed,callback); -必需的 params 参数定义形成动画的 CSS 属性,可选的 speed 参数规定效果的时长; */ $('#carouselUL').animate( {'left' : moveFactor}, 'slow', 'linear', function(){ /* put the first item after the last item */ //:first,:last 是JQuery的选择器; //将第一个li放到最后一个li的后边; $("#carouselUL li:last").after($("#carouselUL li:first")); /* reset left position */ $('#carouselUL').css({'left' : '-340px'}); }); }; /* make the carousel scroll automatically when the page loads */ var moveCarousel = setInterval(autoCarousel, 2000); /* set original thumbnail opacity */ $('.carThumb, #scrollLeft, #scrollRight').css({opacity: 0.5}); /* set up a hover function to animate the opacity and stop the auto-scroll*/ //hover(定义了两个函数,用","号分隔),而且用的不是mouse相关函数 $('.carThumb, #scrollLeft, #scrollRight').hover(function() { //stop(stopAll,goToEnd) 方法用于停止动画或效果,在它们完成之前 $(this).stop().animate({ opacity: 1 }, 75); // end mousein clearInterval(moveCarousel); //pause the auto-scroll on mouse over }, function() { $(this).stop().animate({ opacity: 0.5 }, 250); // end mouseout moveCarousel = setInterval(autoCarousel, 2000); // resume scroll on mouse out }); /* click funtion for right scroll */ $('#scrollRight').click(function(){ /* calculate how far to move the carousel */ var itemWidth = $('#carouselUL li').outerWidth() + 10; var moveFactor = parseInt($('#carouselUL').css('left')) - itemWidth; /* animate the carousel */ $('#carouselUL').animate( {'left' : moveFactor}, 'slow', 'linear', function(){ /* put the first item after the last item */ $("#carouselUL li:last").after($("#carouselUL li:first")); /* reset left position */ $('#carouselUL').css({'left' : '-340px'}); }); }); /* click for left scroll */ $('#scrollLeft').click(function(){ /* calculate movement to the left */ var itemWidth = $('#carouselUL li').outerWidth() + 10; var moveFactor = parseInt($('#carouselUL').css('left')) + itemWidth; /* animate the carousel */ $('#carouselUL').animate( {'left' : moveFactor}, 'slow', 'linear', function(){ /* put the last item before the first */ /* before,after,在被选元素之前、之后插入内容;这里相当于修改了DOM结构; */ $("#carouselUL li:first").before($("#carouselUL li:last")); /* reset the left position */ $('#carouselUL').css({'left' : '-340px'}); }); }); /* click function to load larger pictures */ $('.carThumb').click( function() { /* get the source from our image tag */ var photoInfo = $(this).attr("src"); /* split the source by the '/' */ var photoPathArr = photoInfo.split('/'); /* the path is the first portion of the array plus a forward slash */ var photoPath = photoPathArr[0]+'/'; /* get an array with the photo name in it */ var photoInfoArr = photoInfo.split('_'); /* now put it back together for using in the photo container */ var photoImgTag = '<img src=http://www.mamicode.com/"'+photoPath+photoInfoArr[1]+'" id="currentPhoto" />';>JQuery学习(2)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。