jquery實現自動滾屏效果,適用用公告新聞等滾屏


從網絡上找到的例子,自己做了下擴展,原示例是向上滾動,擴展了一個向下滾動的方法:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>滾屏實驗</title>
<style type="text/css">
ul,li{margin:0;padding:0}
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}
#scrollDiv li{height:25px;padding-left:10px;}
</style>
    <script src="js/jquery-1.4.1.js"></script>

<script type="text/javascript">
    (function ($) {
        $.fn.extend({
            Scroll: function (opt, callback) {
                //參數初始化
                if (!opt) var opt = {};
                var _this = this.eq(0).find("ul:first");
                var lineH = _this.find("li:first").height(), //獲取行高
                        line = opt.line ? parseInt(opt.line, 10) : parseInt(this.height() / lineH, 10), //每次滾動的行數,默認為一屏,即父容器高度
                        speed = opt.speed ? parseInt(opt.speed, 10) : 500, //卷動速度,數值越大,速度越慢(毫秒)
                        timer = opt.timer ? parseInt(opt.timer, 10) : 2000; //滾動的時間間隔(毫秒)
                if (line == 0) line = 1;
                var upHeight = 0 - line * lineH;
                var downHeight=line * lineH - 0;
                //滾動函數
                scrollUp = function () {
                    _this.animate(
                        { marginTop: upHeight },
                        speed,
                        function () {
                            for (i = 1; i <= line; i++) {
                                _this.find("li:first").appendTo(_this);
                            }
                            _this.css({ marginTop: 0 });
                        }
                    );
                },
                //向下滾動函數
                scrollDown = function () {
                    _this.animate(
                        { marginTop: downHeight },//動畫展示css樣式
                        speed,
                        function () {
                            _this.find("li:last").prependTo(_this);
                            _this.css({ marginTop: 0 });
                        }
                        )
                }
                var timerID
                //鼠標事件綁定
                _this.hover(function () {
                    clearInterval(timerID);
                }, function () {
                    timerID = setInterval("scrollDown()", timer);//這里調用向下或者向上滾動函數
                }).mouseout();
            }
        })
    })(jQuery);

    $(document).ready(function () {
        $("#scrollDiv").Scroll({ line: 1, speed: 500, timer: 2000 });
    });
</script>
</head>

<body>
<p>多行滾動演示:</p>
<div id="scrollDiv">
  <ul>
    <li>這是公告標題的第1行</li>
    <li>這是公告標題的第2行</li>
    <li>這是公告標題的第3行</li>
    <li>這是公告標題的第4行</li>
    <li>這是公告標題的第5行</li>
    <li>這是公告標題的第6行</li>
    <li>這是公告標題的第7行</li>
    <li>這是公告標題的第8行</li>
  </ul>
</div>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM