jquery:滚动文本的问题

时间:2013-09-07 04:22:02

标签: javascript jquery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Simple jQuery scrolling function by Max Vergelli</title>
<style>
body, div, p, ul, li {margin: 0px;padding: 0px;}
div.jp-title{
        position:relative;
        height:24px;
        display:block;
        overflow:hidden;
        border:#CCCCCC 1px solid;
    }

      .scrollingtext1{
        position:absolute;
        white-space:nowrap;
        font-family:'Trebuchet MS',Arial;
        font-size:18px;
        font-weight:bold;
        color:#000000;

    }

     .scrollingtext2, .scrollingtext3{
        position:absolute;
        white-space:nowrap;
        font-family:'Trebuchet MS',Arial;
        font-size:18px;
        font-weight:bold;
        color:#000000;
        visibility:hidden;
    }


​​
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
    $( "#clickme" ).click(function() {

        var ob = $('.scrollingtext1');
        var tw = ob.width();
        var ww = ob.parent().width();console.log(tw);
        ob.css({ left: -tw });

        ob.animate({ left: ww}, 20000, 'linear', function() {
            $('.scrollingtext2').css('visibility' ,'visible');
        });  


    });

});
</script>
</head>
<body>
<div class="jp-title">    
    <ul>
    <li class="scrollingtext1">
        scrolling text1: scrolling text1 scrolling text1 scrolling text1 scrolling text1 </li>
    </ul>
     <li class="scrollingtext2">
        scrolling text2: scrolling text2 scrolling text2 scrolling text2 scrolling text2</li>
    </ul>
     <li class="scrollingtext3">
        scrolling text3: scrolling text3 scrolling text3 scrolling text3 scrolling text3</li>
    </ul>
</div>
<div id="clickme">
Click here
</div>
</body>
</html>

问题:

我想要这个功能: 开始时只显示scrollingtext1,当您第一次点击Click here时,scrollingtext1将向右移动,scrollingtext2出现,当您第二次点击{{ 1}},Click here将向右移动,scrollingtext2显示,当您第三次点击scrollingtext3Click here将向右移动时,{{1}显示,循环继续。

以上是我目前的代码,我被困在这里。谁能帮我这个?感谢。

1 个答案:

答案 0 :(得分:0)

你走了。

Fiddle

$('.scrollingtext2,.scrollingtext3').hide();

$("#clickme").click(function () {
    var ob = $('li:visible');
    var tw = ob.width();
    var ww = ob.parent().width();
    ob.css({
        left: -tw
    });

    ob.animate({
        left: ww
    }, 2000, 'linear', function () {
        if (!ob.is(':last-child')) {
            ob.next('li').show().css('left', -tw);
            ob.hide();
        } else {
            ob.parent().find('li:first').show().css('left', -tw);
            ob.hide()
        }
    });
});

$('.scrollingtext2,.scrollingtext3').hide();

$("#clickme").click(function () {
    var ob = $('li:visible');
    var tw = ob.width();
    var ww = ob.parent().width();
    ob.css({
        left: -tw
    });

    ob.animate({
        left: ww
    }, 2000, 'linear', function () {
        if (!ob.is(':last-child')) {
            ob.next('li').show().css('left', 0);
            ob.hide();
        } else {
            ob.parent().find('li:first').show().css('left', 0);
            ob.hide()
        }
    });
});

如果您希望在再次点击之前显示下一行

您的HTML也有语法错误,因此将其更改为

<div class="jp-title">
    <ul>
        <li class="scrollingtext1">scrolling text1: scrolling text1 scrolling text1 scrolling text1 scrolling text1</li>
        <li class="scrollingtext2">scrolling text2: scrolling text2 scrolling text2 scrolling text2 scrolling text2</li>
        <li class="scrollingtext3">scrolling text3: scrolling text3 scrolling text3 scrolling text3 scrolling text3</li>
    </ul>
</div>
<div id="clickme">Click here</div>

从css中删除visibility: hidden,以便我们可以使用jQuery中的选择器告知可见的li

.scrollingtext2, .scrollingtext3 {
    position:absolute;
    white-space:nowrap;
    font-family:'Trebuchet MS', Arial;
    font-size:18px;
    font-weight:bold;
    color:#000000;
}