麻烦这个滑动正确

时间:2013-06-27 17:44:20

标签: javascript jquery

所以这是我的小HTML页面,其中包含视频播放列表:

http://jsfiddle.net/VvR4H/3/

enter image description here

正如你所看到的,在灰色区域我有一个播放列表的内嵌列表,我现在有三个:

  • 袋鼠战斗
  • 澳大利亚体育
  • 真正的足球

在每个播放列表下列出视频。

我想要实现的是我的播放列表之间的水平滚动。现在,当您单击灰色播放列表栏的左侧或右侧角落(文本被切成两半)时,它将滑动到另一个播放列表。

然而滑动不是很好。当你点击左下角时,我想让袋鼠战斗在播放列表栏的中间滑动,你能帮帮我吗?

这是我的HTML:

<div class="container">
    <ul class="playlists">
        <li class="playlist">
            <div class="title"> <span class="move-left">Real Foorball</span>
 <span>Kangaroo Fighting</span>
 <span class="move-right">Australian Sports</span>

            </div>
            <ul class="videos">
                <li class="video">Video 1 C</li>
                <li class="video">Video 2 C</li>
                <li class="video">Video 3 C</li>
            </ul>
        </li>
        <li class="playlist">
            <div class="title"> <span class="move-left">Kangaroo Fighting</span>
 <span>Australian Sports</span>
 <span class="move-right">Real Football</span>

            </div>
            <ul class="videos">
                <li class="video">Video 1 A</li>
                <li class="video">Video 2 A</li>
                <li class="video">Video 3 A</li>
            </ul>
        </li>
        <li class="playlist">
            <div class="title"> <span class="move-left">Australian Sports</span>
 <span>Real Football</span>
 <span class="move-right">Kangaroo Fighting</span>

            </div>
            <ul class="videos">
                <li class="video">Video 1 B</li>
                <li class="video">Video 2 B</li>
                <li class="video">Video 3 B</li>
            </ul>
        </li>
    </ul>
</div>

我的CSS:

ul li {
    list-style: none;
}
.container {
    position: relative;
    background: #000;
    width: 300px;
    height: 500px;
    overflow: hidden;
    font-family: sans-serif;
}
ul.playlists {
    width: 1200px;
    padding-left: 0;
    margin-top: 0;
    position: absolute;
    left: -300px;
}
ul.playlists li {
    float: left;
    width: 300px;
    height: 50px;
}
ul.playlists li.playlist .title {
    width: 100%;
    background: grey;
    color: white;
    line-height: 50px;
    text-align: center;
}
ul.playlists li.playlist .title .move-left, ul.playlists li.playlist .title .move-right {
    width: 30px;
    line-height: 50px;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
}
ul.playlists li.playlist .title .move-left {
    float: left;
    direction: rtl;
}
ul.playlists li.playlist .title .move-right {
    float: right;
}
ul.videos {
    clear: both;
    padding-left: 0;
}
ul.videos li {
    float: left;
    width: 250px;
    height: 50px;
    padding: 25px;
    color: white;
    background: blue;
    border-top: 1px solid black;
}

我的JavaScript:

$(".move-left").click(function () {
    $(this).parent().parent().parent().animate({
        "left": "0"
    }, 500, "linear", function () {
        console.log("yay");
    });
});

$(".move-right").click(function () {
    $(this).parent().parent().parent().animate({
        "left": "-600"
    }, 500, "linear", function () {
        console.log("yay");
    });
});

1 个答案:

答案 0 :(得分:1)

它不完整(我没有做无限循环),但我猜它有更好的动画,如你所要求的。

我将你的html分成两部分。第一个是移动慢,然后是第二个。因此,您可以查看下一个标题的一半文本,但您不必复制文本。

http://jsfiddle.net/VvR4H/10/

HTML

<div class="container">
    <div class="title-wrapper">
            <div class="title"> 
                <span>Real Foorball</span>
                <span>Kengoroo Fighting</span>
                <span>Australian Sports</span>
            </div>
    </div>


    <div class="playlist">
            <ul class="videos">
                <li class="video">Video 1 C</li>
                <li class="video">Video 2 C</li>
                <li class="video">Video 3 C</li>
            </ul>
            <ul class="videos">
                <li class="video">Video 1 A</li>
                <li class="video">Video 2 A</li>
                <li class="video">Video 3 A</li>
            </ul>
            <ul class="videos">
                <li class="video">Video 1 B</li>
                <li class="video">Video 2 B</li>
                <li class="video">Video 3 B</li>
            </ul>
        </div>
</div>

CSS

    ul li {
    list-style: none;
}

.container {
    position: relative;
    background: #000;
    width: 300px;
    height: 500px;
    font-family: sans-serif;
    overflow: hidden;
}

div.title-wrapper {
    background: grey;
    height: 50px;
}

div.title {
    position: absolute;
    overflow: hidden;
    height: 50px;
    left: 75px;
    white-space: nowrap;
}

div.playlist {
    position: absolute;
    overflow: hidden;
    top: 50px;
    clear: both;
    white-space: nowrap;
}
div.title span {
    width: 300px;
    background: grey;
    color: white;
    line-height: 50px;
    text-align: left;
    display: block;
    float: left;
    margin: 0;
    padding: 0;
    text-indent: 20px;
}

ul.videos {
    float: left;    
    width: 300px;
    margin: 0;
    padding: 0;
}

ul.videos li {
    display: block;
    background: blue;
    height: 50px;
    padding: 10px 20px;
    margin: 1px 0 0 0;
    color: white;
}

的javascript

$('.title span').css({
    'text-indent' : '0', 
    'text-align' : 'center', 
    'width' : '150px'
});

var titles = [];
$('.title span').each( function () {
    titles.push($(this));
});

var max = titles.length-1;
var left = max;
var right = 1;

$('.title span').click(function () {
    console.log($(this).context.innerText + '   left: ' +titles[left].context.innerText + '   right: ' +titles[right].context.innerText)
    if($(this).context==titles[left].context) {
        left = (left==0) ? max : --left;
        right = (right==0) ? max : --right;
        $('.title').animate({
            "left": "+=150px"
        }, 500);
        $('.playlist').animate({
            "left": "+=300px"
        }, 500);
    } 
    if($(this).context==titles[right].context) {
        left = (left==max) ? 0 : ++left;
        right = (right==max) ? 0 : ++right;
        $('.title').animate({
            "left": "-=150px"
        }, 500);
        $('.playlist').animate({
            "left": "-=300px"
        }, 500);
    }
});