jQuery + css 公告從左往右滾動


$(function() {
    // 公告滾動
    $(".notice-content").textScroll();
});

/**
 * 從右往左滾動文字
 * @returns {undefined}
 */
$.fn.textScroll = function() {
    // 滾動步長(步長越大速度越快)
    var step_len = 60;
    var this_obj = $(this);
    var child = this_obj.children();
    var this_width = this_obj.width();
    var child_width = child.width();
    var continue_speed = undefined;// 暫停后恢復動畫速度
    // 初始文字位置
    child.css({
        left: this_width
    });

    // 初始動畫速度speed
    var init_speed = (child_width + this_width) / step_len * 1000;

    // 滾動動畫
    function scroll_run(continue_speed) {
        var speed = (continue_speed == undefined ? init_speed : continue_speed);
        child.animate({
            left: -child_width
        }, speed, "linear", function() {
            $(this).css({
                left: this_width
            });
            scroll_run();
        });
    }

    // 鼠標動作
    child.on({
        mouseenter: function() {
            var current_left = $(this).position().left;
            $(this).stop();
            continue_speed = (-(-child_width - current_left) / step_len) * 1000;
        },
        mouseleave: function() {
            scroll_run(continue_speed);
            continue_speed = undefined;
        }
    });

    // 啟動滾動
    scroll_run();
};

  

                <div class="notice-title">公告:</div>
                <div class="notice-content">
                    <div class="notice-text"><span>公告內容</span></div>
                </div>

  

.notice-title {
            color: #fff;
        }

        .notice-content {
            position: relative;
            width: 800px;
            height: 30px;
            white-space: nowrap;
            overflow: hidden;
            float: left;
            margin-left: 55px;
            margin-top: -30px;

            /*背景透明度
            background-color: #fff;
            filter:alpha(opacity=10);
            -moz-opacity:0.5;
            -khtml-opacity: 0.5;
            opacity: 0.5;
            */
        }
        .notice-text {
            color: red;
            font-size: 14px;
            position: absolute;
        }

  


免責聲明!

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



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