0
<!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显示,第二次点击右移,第三次显示单击,将向右移动并显示,然后循环继续。scrollingtext1scrollingtext2Click herescrollingtext2scrollingtext3Click herescrollingtext3scrollingtext1

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

4

1 回答 1

0

干得好。

小提琴

$('.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>

visibility: hidden从 css 中删除 s,以便我们可以li使用 jQuery 中的选择器将不可见的 s 与可见的 s 区分开来

.scrollingtext2, .scrollingtext3 {
    position:absolute;
    white-space:nowrap;
    font-family:'Trebuchet MS', Arial;
    font-size:18px;
    font-weight:bold;
    color:#000000;
}
于 2013-09-07T04:46:54.087 回答