以jQuery模擬跑馬燈<margeen>的範例

  • 16966
  • 0
  • 2010-09-27

以jQuery模擬跑馬燈<margeen>的範例

在<head>區段中先置入以下的語法

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</script>
<script type="text/javascript">
$(function() {
            var marquee = $("#marquee");
            marquee.css({ "overflow": "hidden", "width": "100%" });

            // wrap "My Text" with a span (IE doesn't like divs inline-block)
            marquee.wrapInner("<span>");
            marquee.find("span").css({ "width": "50%", "display": "inline-block", "text-align": "center" });
            marquee.append(marquee.find("span").clone()); // now there are two spans with "My Text"

           // marquee.wrapInner("<div>");
          //  marquee.find("div").css("width", "");
         
                          marquee.wrapInner("<div>");
                //設定div 的width
                var width = 85 * 3 + "%";
                marquee.find("div").css("width", width);

        //    var reset = function() {
          //      $(this).css("margin-left", "0%");
         //       $(this).animate({ "margin-left": "-100%" }, 15000, 'linear', reset);
        //    };
    var margin_left = "-"+85 * 1.5 + "%";
                var reset = function () {
                    $(this).css("margin-left", "0%");
                    $(this).animate({ "margin-left": margin_left }, 12000, 'linear', reset);
                };
            reset.call(marquee.find("div"));
        });
</script>

Dotlogs Tags:
將<div>的ID指定為marrgen即可
<div id="marquee" >Your html …</div>

 

實例 Demo: http://web.systemlead.com/富鼎/test_marquee_Robin.htm

直接下載可執行範例:https://dotblogsfile.blob.core.windows.net/user/robin/1009/201092725018764.zip

注意事項

  1. <div id=”marquee”>若有<div> 或 <Table>可能會有跑位的現象
  2. jQuery中的紅字部份可以調整速度
  3. 目前在IE 8, IE9, Chrome, Firefox測試正常, 但IE 7 尚有跑位的問題正在克服中