頁面效果簡單做(不斷收集更新),頁面效果收集更新

來源:互聯網
上載者:User

頁面效果簡單做(不斷收集更新),頁面效果收集更新

簡介:有自己寫的,也有網上看到的,即使是別人寫的也會對其改動,或添加註釋,並保持統一的編碼風格,便於閱讀。目標是不用庫即可完成,簡單做,能夠闡述邏輯和原理清楚即可,因此可能考慮不是最周詳的,包括跨瀏覽器的問題,如果你打算使用在項目中使用最好測試清楚,還可能要進一步修改之。

注意:打算提供線上例子的,但短時間內沒有靜態空間,所以例子的事情要稍等一陣子。已提供線上例子。

1、簡易拖放效果

使用了 DOM 1 的方式登記事件,其實無必要 addEventListener,因為根據滑鼠事件,同一時刻通常 document 只有一個 mousemove/mouseup 事件。點擊查看例子。

<meta charset="utf-8" />  <title>拖放 DD</title>  <script>       SimpleDrag = function(el) {          this.el = el;          this._x = this._y = 0;          el.onmousedown = delegate(this, this.start);          this.move = delegate(this, this.move);                    function delegate(object, fn){ // 綁定當前 this,並且修正瀏覽器安全色問題 e ||  window.event            return function(e) {                    return fn.call(object, (e || window.event));                }          }      };      SimpleDrag.prototype = {          start : function(e) {              this._x = e.clientX - this.el.offsetLeft;              this._y = e.clientY - this.el.offsetTop;              document.onmousemove = this.move;              document.onmouseup = this.stop;          },          // 拖動            move : function(e) {              this.el.style.left = e.clientX - this._x + "px";              this.el.style.top  = e.clientY - this._y + "px";          },          // 停止拖動            stop : function() {              document.onmousemove = document.onmouseup = null;          }      };  </script>  <style>      .dragable{          background-color:#C4E3FD;           width:50px; height:50px;          position:absolute;          left: 20px;          cursor:move;      }      .dragEl_1{          top : 10px;          border:5px solid blue;       }      .dragEl_2{          top : 80px;          border:5px solid red;       }  </style>  <div class="dragable dragEl_1">1</div>    <div class="dragable dragEl_2">2</div>    <script>      new SimpleDrag(document.querySelector(".dragEl_1"));        new SimpleDrag(document.querySelector(".dragEl_2"));    </script> 
2、上下滾動內容

原理是取出最尾元素放到前頭。點擊查看例子。

<html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>上下滾動內容</title>  </head>  <body>  <ul id="scroll">  <li>11111111111</li>  <li>22222222222</li>  <li>33333333333</li>  <li>44444444444</li>  <li>55555555555</li>  <li>66666666666</li>  <li>77777777777</li>  <li>88888888888</li>  <li>99999999999</li>  <li>00000000000</li>  </ul>      <ol id="scrollA">  <li>11111111111</li>  <li>22222222222</li>  <li>33333333333</li>  <li>44444444444</li>  <li>55555555555</li>  <li>66666666666</li>  <li>77777777777</li>  <li>88888888888</li>  <li>99999999999</li>  <li>00000000000</li>  </ol>  <script>/** * @param {Element} el 列表元素 * @param {Number} interval 動畫時間間隔 * @param {Boolean} canstop 是否可以滑鼠移入時候暫停動畫 */function ScrollContent(el, interval, canstop) {interval = interval || 3000;canstop  = canstop  || false;function scroll() {var lastEl = el.firstChild;while (lastEl.nodeType != 1)  // 找到最後一個元素lastEl = lastEl.nextSibling;el.appendChild(el.removeChild(lastEl)); // 把最後一個元素放到前頭}var t = window.setInterval(scroll, interval);if (canstop) {el.onmouseover = function() {if (t) window.clearInterval(t);}el.onmouseout = function() {t = window.setInterval(scroll, interval);}}}new ScrollContent(document.getElementById('scroll'), 1000);new ScrollContent(document.getElementById('scrollA'), 500, true);</script></body>  </html> 
3、左右滾動內容

原理跟前者一樣,只不過是把元素換為字串,應該是更簡單的了。點擊查看線上例子。



<meta charset="utf-8" /><title>水平字幕滾動</title><div class="content1">這是一段滾動的文字11111111</div><div class="content2">這是一段滾動的文字22222222</div><script>/** * @param {Element} el 列表元素 * @param {Number} interval 動畫時間間隔 * @param {Boolean} canstop 是否可以滑鼠移入時候暫停動畫 */function ScrollContent_Hoz(el, interval, canstop) {interval = interval || 500;canstop  = canstop  || false;var arr = el.innerHTML.split("");function scroll() {arr.push(arr.shift());el.innerHTML = arr.join("");}var t = window.setInterval(scroll, interval);if (canstop) {el.onmouseover = function() {if (t) window.clearInterval(t);}el.onmouseout = function() {t = window.setInterval(scroll, interval);}}} new ScrollContent_Hoz(document.querySelector('.content1'));new ScrollContent_Hoz(document.querySelector('.content2'), null, true);</script>
3、多級聯動下拉式功能表

聯動本身並無多大痛點,只是 onchange 事件的運用即可。值得一提的是這個示範了日曆控制項所涉及的日曆產生演算法——當然這是後話了。點擊查看線上例子。

<meta charset="UTF-8"><title>聯動 Select 下拉</title><select id="year"><option value="0">--請選擇--</option></select>年<select id="month"><option value="0">--請選擇--</option></select>月<select id="day"><option value="0">--請選擇--</option></select>日<script>function initSelect(selectEl, i, end) {selectEl.length = 1;for (; i <= end; i++) {try {selectEl.add(new Option(i, i), null);} catch (e) {selectEl.add(new Option(i, i));}}}var year = document.getElementById("year");var month = document.getElementById("month");var day = document.getElementById("day");initSelect(year, 1970, 2012);year.onchange = function() {if (this.value != 0) {initSelect(month, 1, 12);} else {month.length = 1;day.length = 1;}}month.onchange = function() {if (this.value != 0) {var m30 = {2 : 1,4 : 1,6 : 1,9 : 1,11 : 1};if (this.value == 2) { // 二月份特別處理if (isLeapYear(year.value))  initSelect(day, 1, 29);else initSelect(day, 1, 28);} else if (this.value in m30) // 三十天的月份/*因為2、4、6、9、11月份都是30天,如果把它們放在一個數組中,那麼還要遍曆來判斷是否相等,則比較麻煩了,於是在這裡把這些都當成對象來處理,使用 in 判斷即可*/ initSelect(day, 1, 30);else  initSelect(day, 1, 31);} else day.length = 1;}// 判斷閏年的條件:能被4整除且不能被100整除 或 能被100整除且能被400整除function isLeapYear(y) {return (y % 4 == 0 && y % 100 != 0)|| (y % 100 == 0 && y % 400 == 0);}</script>
http://www.zgwlsc.net/lstore/static/src/widget/carousel/main.js

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.