我是 Javascript(jQuery) 领域的新手,我似乎真的不知道自己在做什么。
所以我决定问你!
我这里有这段 Javascript
$(document).ready(function() {
//move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
$('#carousel-1 #carousel_ul li:first').before($('#carousel-1 #carousel_ul li:last'));
//when user clicks the image for sliding right
$('#carousel-1 .navNext').click(function(){
//get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '
var item_width = $('#carousel-1 #carousel_ul li').outerWidth() + 18;
//calculae the new left indent of the unordered list
var left_indent = parseInt($('#carousel-1 #carousel_ul').css('left')) - item_width;
//make the sliding effect using jquery's anumate function '
$('#carousel-1 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
//get the first list item and put it after the last list item (that's how the infinite effects is made) '
$('#carousel-1 #carousel_ul li:last').after($('#carousel-1 #carousel_ul li:first'));
//and get the left indent to the default -210px
$('#carousel-1 #carousel_ul').css({'left' : '-179px'});
});
});
//when user clicks the image for sliding left
$('#carousel-1 .navPrev').click(function(){
var item_width = $('#carousel-1 #carousel_ul li').outerWidth() + 18;
/* same as for sliding right except that it's current left indent + the item width (for the sliding right it's - item_width) */
var left_indent = parseInt($('#carousel-1 #carousel_ul').css('left')) + item_width;
$('#carousel-1 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
/* when sliding to left we are moving the last item before the first list item */
$('#carousel-1 #carousel_ul li:first').before($('#carousel-1 #carousel_ul li:last'));
/* and again, when we make that change we are setting the left indent of our unordered list to the default -210px */
$('#carousel-1 #carousel_ul').css({'left' : '-179px'});
});
});
});
$(document).ready(function() {
//move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
$('#carousel-2 #carousel_ul li:first').before($('#carousel-2 #carousel_ul li:last'));
//when user clicks the image for sliding right
$('#carousel-2 .navNext').click(function(){
//get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '
var item_width = $('#carousel-2 #carousel_ul li').outerWidth() + 18;
//calculae the new left indent of the unordered list
var left_indent = parseInt($('#carousel-2 #carousel_ul').css('left')) - item_width;
//make the sliding effect using jquery's anumate function '
$('#carousel-2 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
//get the first list item and put it after the last list item (that's how the infinite effects is made) '
$('#carousel-2 #carousel_ul li:last').after($('#carousel-2 #carousel_ul li:first'));
//and get the left indent to the default -210px
$('#carousel-2 #carousel_ul').css({'left' : '-179px'});
});
});
//when user clicks the image for sliding left
$('#carousel-2 .navPrev').click(function(){
var item_width = $('#carousel-2 #carousel_ul li').outerWidth() + 18;
/* same as for sliding right except that it's current left indent + the item width (for the sliding right it's - item_width) */
var left_indent = parseInt($('#carousel-2 #carousel_ul').css('left')) + item_width;
$('#carousel-2 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
/* when sliding to left we are moving the last item before the first list item */
$('#carousel-2 #carousel_ul li:first').before($('#carousel-2 #carousel_ul li:last'));
/* and again, when we make that change we are setting the left indent of our unordered list to the default -210px */
$('#carousel-2 #carousel_ul').css({'left' : '-179px'});
});
});
});
$(document).ready(function() {
//move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
$('#carousel-3 #carousel_ul li:first').before($('#carousel-3 #carousel_ul li:last'));
//when user clicks the image for sliding right
$('#carousel-3 .navNext').click(function(){
//get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '
var item_width = $('#carousel-3 #carousel_ul li').outerWidth() + 18;
//calculae the new left indent of the unordered list
var left_indent = parseInt($('#carousel-3 #carousel_ul').css('left')) - item_width;
//make the sliding effect using jquery's anumate function '
$('#carousel-3 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
//get the first list item and put it after the last list item (that's how the infinite effects is made) '
$('#carousel-3 #carousel_ul li:last').after($('#carousel-3 #carousel_ul li:first'));
//and get the left indent to the default -210px
$('#carousel-3 #carousel_ul').css({'left' : '-179px'});
});
});
//when user clicks the image for sliding left
$('#carousel-3 .navPrev').click(function(){
var item_width = $('#carousel-3 #carousel_ul li').outerWidth() + 18;
/* same as for sliding right except that it's current left indent + the item width (for the sliding right it's - item_width) */
var left_indent = parseInt($('#carousel-3 #carousel_ul').css('left')) + item_width;
$('#carousel-3 #carousel_ul:not(:animated)').animate({'left' : left_indent},250,function(){
/* when sliding to left we are moving the last item before the first list item */
$('#carousel-3 #carousel_ul li:first').before($('#carousel-3 #carousel_ul li:last'));
/* and again, when we make that change we are setting the left indent of our unordered list to the default -210px */
$('#carousel-3 #carousel_ul').css({'left' : '-179px'});
});
});
});
你可以在这里查看它是如何工作的。
我真正想知道的是可以减少我们使用的 ID 数量吗?
因为截至目前,我们有#carousel-1 + 2 + 3
很多看似不必要的 ID。
基本上,我们试图使 JS 代码比实际更小。
预先感谢!