當滾動的內容不少,好比鬧鐘裏設置秒,一共有60項。讓使用者從59ms滾回01ms是一件很痛苦的事情,因此:
在列表項太多的狀況下,咱們但願可以有個無限循環的滾動。00ms和01ms是無縫連接起來的。以下圖所示:css
http://alloyteam.github.io/AlloyTouch/select/infinite/html
先引用依賴的JS和CSS文件。git
<link rel="stylesheet" href="select.css" /> <script src="../../transformjs/transform.js"></script> <script src="../../alloy_touch.js"></script> <script src="alloy_touch.select.infinite.js"></script>
而後:github
var i = 0, options = []; for (; i < 60; i++) { options.push({ value: i, text: i < 10 ? "0" + i+" ms" : i + " ms" }); } var iselect = new AlloyTouch.Select({ options: options, selectedIndex: 11, change: function (item, index) { }, complete: function (item, index) { document.body.insertAdjacentHTML("beforeEnd", "<br/>選了第" + index + "項<br/>value:" + item.value + "<br/>text:" + item.text); } }) iselect.show();
在看原理以前,咱們看下dom裏面的屬性變化。dom
new AlloyTouch({ touch: container, target: { y: -1 * preSelectedIndex * step }, property: "y", vertical: true, step: step, change: function (value) { correction(value); }, touchStart: function (evt, value) { }, touchMove: function (evt, value) { }, touchEnd: function (evt, value) { }, tap: function (evt, value) { }, pressMove: function (evt, value) { }, animationEnd: function (value) { } }) function correction(value) { value %= scrollerHeight; if (Math.abs(value) > scrollerHeight-90) { if (value > 0) { value -= scrollerHeight; } else { value += scrollerHeight; } } scroll.translateY = value - scrollerHeight; }
能夠看到初始化AlloyTouch實例的時候已經不存在min和max的參數,由於不須要回彈。
經過correction去產生跳動週期的效果。(注意:雖然值會跳一個週期,可是dom的渲染表現是看不出跳動的)
其中target是一個包含y屬性的對象字面量,
scroll是滾動的對象,被mix過transfrom的相關屬性,因此能夠直接經過scroll.translateY 設置其垂直方向上的位移。插件
由於不是旋轉360自動會處理週期,咱們本身經過運動對象字面量{y:xx},而後經過correction映射到滾動對象的translateY來控制週期性。
後續還會給你們帶來:code
https://github.com/AlloyTeam/AlloyTouchorm
你要觸摸的一切都在這裏。htm