DIV CSS 佈局教程網

動手做 jQuery 左右滾動菜單:滑過連續捲動、按下加速
編輯:關於JavaScript     

菜單項目一多,版面就塞不下。與其換行折疊,不如做成左右滾動的橫向清單。這個以 jQuery 實作的滾動菜單,支援滑鼠移入連續捲動、按住加速、內容不足時自動隱藏箭頭,還能自訂樣式類別。以下是完整做法。

scroll­able.js

scrollable = function (content, render, options, beforeScroll) {
  /*
   * 使用注意:
   * 1. content 必須自行指定寬度;內部元素若為塊級元素,請用 float:left 排成一列。
   * 2. 強烈建議自訂樣式,以便融入既有設計。
   *
   * 參數說明
   * content      : 內容元素,選擇器或 jQuery 物件
   * render       : 渲染目標容器
   * options      : 選項
   *   scrollable_class          整體外框樣式,預設 ui-scrollable
   *   scrollable_left_class     左按鈕樣式,預設 ui-scrollable-left
   *   scrollable_container_class 內容容器樣式,預設 ui-scrollable-container
   *   scrollable_right_class    右按鈕樣式,預設 ui-scrollable-right
   *   delay      滑鼠停留時兩次移動的間隔(毫秒)
   *   speed      滑過時單次移動距離
   *   speedup    按下時單次移動距離
   *   resizeEvent 是否監聽視窗尺寸變化,預設 false
   * beforeScroll : 滾動前的回呼,可藉此放寬邊界限制
   */
  options.scrollable_class = options.scrollable_class || 'ui-scrollable';
  options.scrollable_left_class = options.scrollable_left_class || 'ui-scrollable-left';
  options.scrollable_container_class = options.scrollable_container_class || 'ui-scrollable-container';
  options.scrollable_right_class = options.scrollable_right_class || 'ui-scrollable-right';
  options.leftText = options.leftText || '';
  options.rightText = options.rightText || '';
  options.delay = options.delay || 20;
  options.speed = options.speed || 5;
  options.speedup = options.speedup || 10;
  options.resizeEvent = options.resizeEvent || false;

  render = (typeof render == 'string' ? $(render) : render);
  content = (typeof content == 'string' ? $(content) : content);

  var scrollable = $('<div></div>')
    .attr('id', 'scrollable_' + content.attr('id'))
    .attr('className', options.scrollable_class);
  var left = $('<div></div>')
    .attr('id', 'scrollable_left_' + content.attr('id'))
    .attr('className', options.scrollable_left_class);
  left.text(options.leftText);
  var container = $('<div></div>')
    .attr('id', 'scrollable_container_' + content.attr('id'))
    .attr('className', options.scrollable_container_class);
  content.css('line-height', '29px')
    .css('position', 'relative').css('left', '0px')
    .css('overflow', 'hidden').css('float', 'left');
  var right = $('<div></div>')
    .attr('id', 'scrollable_right_' + content.attr('id'))
    .attr('className', options.scrollable_right_class);
  right.text(options.rightText);

  function show() {
    scrollable.appendTo(render);
    container.appendTo(scrollable);
    left.css('display', ''); right.css('display', '');
    content.appendTo(container);
    left.prependTo(scrollable);
    right.appendTo(scrollable);
    // 內容不夠寬就乾脆把箭頭藏起來
    if (content.width() <= container.width() + 20) {
      left.css('display', 'none');
      right.css('display', 'none');
      container.width(content.width());
      scrollable.width(container.width());
    }
    container.position = { left: container.css('left').substr(0, -2) };
    container.position.right = container.position.left + container.width();
    content.position = { left: new Number(content.css('left').substr(0, -2)) };
    content.position.right = content.position.left + content.width();
  }
  show();

  var scroll = false;
  function move(distance) {
    var newLeft = content.position.left + distance;
    var newRight = content.position.right + distance;
    if (distance > 0 && newLeft > container.position.left) {
      distance = container.position.left - content.position.left;
      scroll = false;                       // 撞到左邊界
    } else if (distance < 0 && newRight < container.position.right) {
      distance = content.position.right - container.position.right;
      scroll = false;                       // 撞到右邊界
    }
    newLeft = content.position.left + distance;
    newRight = content.position.right + distance;
    scroll = beforeScroll
      ? beforeScroll({ left: content.position.left, right: content.position.right },
                     { left: newLeft, right: newRight })
      : scroll;
    if (scroll) {
      content.css('left', newLeft + 'px');
      content.position.left += distance;
      content.position.right += distance;
      setTimeout('move(' + distance + ')', options.delay);
    }
  }

  left.mouseover(function () { scroll = true; move(options.speed); });
  right.mouseover(function () { scroll = true; move(-options.speed); });
  left.mouseout(function () { scroll = false; });
  right.mouseout(function () { scroll = false; });
  left.mousedown(function () { scroll = true; move(options.speedup); });
  right.mousedown(function () { scroll = true; move(-options.speedup); });
  left.mouseup(function () { scroll = false; });
  right.mouseup(function () { scroll = false; });
}

原理不複雜:用 setTimeout 自呼叫 move() 形成連續位移,靠一個 scroll 旗標控制何時停;抵達邊界時把旗標設為 false,動畫自然收住。

Default.aspx

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery 左右滾動菜單</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="scrollable.js"></script>
<style type="text/css">
.button{cursor:pointer;}
.button:hover > * {background-position: 0 -42px;}
.button_left{float:left; background:url(menu_out_left.gif) no-repeat 0 0; width:4px; height:26px;}
.button_center{float:left; background:url(menu_out_bj.gif) repeat-x 0 0; width:80px; text-align:center}
.button_right{float:left; background:url(menu_out_right.gif) no-repeat 0 0; width:4px; height:26px;}
.ui-scrollable{width:800px; height:29px;}
.ui-scrollable-container{float:left; width:780px; height:inherit; position:relative; overflow:hidden; border-bottom:1px solid #DDDDDD;}
.ui-scrollable-content{float:left; width:1770px; height:inherit;}
.ui-scrollable-left{float:left; background:url(scrollable_left_out.gif) no-repeat 0 0; width:10px; height:29px; cursor:pointer;}
.ui-scrollable-right{float:left; background:url(scrollable_right_out.gif) no-repeat 0 0; width:10px; height:29px; cursor:pointer;}
.ui-scrollable-left:hover{background:url(scrollable_left_on.gif) no-repeat 0 0;}
.ui-scrollable-right:hover{background:url(scrollable_right_on.gif) no-repeat 0 0;}
</style>
<script type="text/javascript">
$(function () {
  scrollable('#scrollable_content', '#scrollable_render', {}, function (originalPosition, newPosition) {
    return true;
  });
});
</script>
</head>
<body>
<center>
<div id="scrollable_render" class="scrollable-render"></div>
<div id="scrollable_content" class="ui-scrollable-content">
  <div class="button"><div class="button_left"></div>
    <div class="button_center">菜單一</div><div class="button_right"></div></div>
  <div class="button"><div class="button_left"></div>
    <div class="button_center">菜單二</div><div class="button_right"></div></div>
  <div class="button"><div class="button_left"></div>
    <div class="button_center">菜單三</div><div class="button_right"></div></div>
  <!-- 依此類推,菜單四到菜單十可自行複製 -->
</div>
</center>
</body>
</html>

每個按鈕拆成左、中、右三層,是為了讓中間文字區能伸縮而兩端圓角不變形。最後別忘了引入 jQuery 本體,這裡用的是 1.4.2。整套元件不依賴任何後端,直接丟進既有頁面就能用。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved