首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery Carousel未循环

Jquery Carousel未循环
EN

Stack Overflow用户
提问于 2012-07-10 03:54:42
回答 1查看 242关注 0票数 0

我目前正在开发一个jQuery carousel,它在所有浏览器中都工作得很好,除了最后一项和第一项不会循环来创建无限的carousel之外

这是js

代码语言:javascript
复制
$(document).ready(function() {
    //move the last list item before the first item. The purpose of this is if the user clicks previous he will be able to see the last item.
    $('#profile-container ul li:first').before($('#profile-container ul li:last'));

    //when user clicks the image for sliding right
    $('#right_scroll img').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 = $('#profile-container ul li').outerWidth() + 20;

        //calculate the new left indent of the unordered list
        var left_indent = parseInt($('#profile-container ul').css('left')) - item_width;

        //make the sliding effect using jquery's animate function
        $('#profile-container ul').animate({'left' : left_indent},{queue:false, duration:500},function() {

            //THIS LINE ISN'T WORKING (it's how the infinite effects is made)
            $('#profile-container ul li:last').after($('#profile-container ul li:first'));

            //THIS LINE ISN'T WORKING
            $('#profile-container ul').css({'left' : '-100px'});
        });
    });

    //when user clicks the image for sliding left
    $('#left_scroll img').click(function(){

        var item_width = $('#profile-container ul li').outerWidth() + 20;

        /* 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($('#profile-container ul').css('left')) + item_width;

        $('#profile-container ul').animate({'left' : left_indent},{queue:false, duration:500},function(){

        /* THIS LINE ISN'T WORKING */
        $('#profile-container ul li:first').before($('#profile-container ul li:last'));

        /* THIS LINE ISN'T WORKING /*
        $('#profile-container ul').css({'left' : '-100px'});
        });

    });
});

由于某些原因,//get第一个列表项行不起作用。

这是html

代码语言:javascript
复制
    <div id="profile-wrapper">
        <div id='left_scroll'><img src="<?php echo get_template_directory_uri(); ?>/library/images/left.png" /></div>  
        <div id="profile-container">
            <ul>
                <li><a href="#" class="profile-image image-1"></a></li>
                <li><a href="#" class="profile-image image-2"></a></li> 
                <li><a href="#" class="profile-image image-3"></a></li>
                <li><a href="#" class="profile-image image-4"></a></li>
                <li><a href="#" class="profile-image image-5"></a></li>
            </ul>
        </div>
        <div id='right_scroll'><img src="<?php echo get_template_directory_uri(); ?>/library/images/right.png" /></div>
    </div>

最后是css

代码语言:javascript
复制
#profile-wrapper {
display: table;  
margin: 20px auto;
}

#profile-container {
float:left; /* important for inline positioning */  
width: 390px; /* important (this width = width of list item(including margin) * items shown */  
overflow: hidden;  /* important (hide the items outside the div) */ 
}

#profile-container ul {
position: relative;
left: -95px; /* important (this should be negative number of list items width(including margin) */
list-style-type: none; /* removing the default styling for unordered list items */
width:9999px; /* important */
/* non-important styling bellow */
margin-top: 20px;
padding-bottom:10px;
}

#profile-container ul li {
position: relative;
float: left; /* important for inline positioning of the list items */
width:80px;  /* fixed width, important */
/* non-important styling bellow */
background-color: #f5f5f5;
height: 80px;
border-radius: 95px;
-khtml-border-radius: 95px;
-moz-border-radius: 95px;
-webkit-border-radius: 95px;
margin-right: 20px;
z-index: 100;
}

#left_scroll, #right_scroll {
height: 80px; 
width: 80px;
-khtml-border-radius: 95px;
-moz-border-radius: 95px;
-webkit-border-radius: 95px;
position: relative;
z-index: 100;
float: left;
margin: 20px 20px;
}

#left_scroll img, #right_scroll img{
/*styling*/
cursor: pointer;
cursor: hand;
}

.image-1 {
background: url('../../library/images/profiles/1.jpg');
background-position: center;
}

.image-2 {
background: url('../../library/images/profiles/2.jpg');
background-position: center;
}

.image-3 {
background: url('../../library/images/profiles/3.jpg');
background-position: center;
}

.image-4 {
background: url('../../library/images/profiles/4.jpg');
}

.image-5 {
background: url('../../library/images/profiles/5.jpg');
background-position: center;
}

.profile-image {
display: block;
height: 70px;
width: 70px;
position: relative;
top: 5px;
left: 5px;
border-radius: 90px;
-khtml-border-radius: 90px;
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
}

任何输入都会很有帮助...我想我什么都说过了。

非常感谢

克里斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-10 16:50:57

替换以下两个实例:

代码语言:javascript
复制
$('#profile-container ul').animate({'left' : left_indent},{queue:false, duration:500},function() {

通过以下方式:

代码语言:javascript
复制
$('#profile-container ul').animate({'left' : left_indent},500,function() {  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11402149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档