>本文詳細介紹了構建用於檢測觸摸設備上水平滑動的jQuery插件。 第一部分著重於創建響應式圖像旋轉木馬。第二部分(此處不包括)會添加滑動檢測。
>密鑰概念:
- 該教程創建了一個jQuery插件,該插件檢測水平滑動,主要是通過圖像旋轉木馬進行演示的。
- 核心邏輯位於 類中,處理瀏覽器事件並觸發回調。 該插件使用閉合來防止命名衝突。
-
Swiper
>教程解釋了插件實現,包括設置輪播限制,跟踪位置以及使用JSON。 - > html&css:
相關的CSS樣式旋轉木馬:
> div的400%寬度可容納四個圖像,而每個圖像的分佈均為25%。 根據需要調整這些值的不同圖像計數或尺寸。
><div style="width: 330px; height: 200px;"> <div id="target"> <div class="frame"> <div class="pictures"> <div class="pic"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791350855.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791497040.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791447095.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791526338.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> </div> </div> </div> </div>
> javaScript(插件骨架):
img { width: 100%; margin: 0; } .frame { width: 100%; height: 100%; border: 1px solid #ccc; overflow: hidden; position: relative; } .pictures { position: absolute; width: 400%; left: 0%; } .pictures:after { content: "<pre class="brush:php;toolbar:false"><code class="language-javascript">(function ($) { 'use strict'; var Swiper = function (el, callbacks) { // Constructor logic (detailed below) }; $.fn.swiper = function (callbacks) { if (typeof callbacks.swiping !== 'function') { throw '"swiping" callback must be defined.'; } this.each(function () { var tis = $(this), swiper = tis.data('swiper'); if (!swiper) { tis.data('swiper', (swiper = new Swiper(this, callbacks))); } }); }; }(jQuery));20"; display: none; height: 0; } .pictures .pic { width: 25%; float: left; } jQuery插件的基本結構:
.pictures
.pic
這建立了插件結構。
swiper class(部分實現):
>
var Swiper = function (el, callbacks) { var tis = this; this.el = el; this.cbs = callbacks; this.points = [0, 0]; this.el.addEventListener('touchstart', function (evt) { tis.start(evt); }); this.el.addEventListener('touchmove', function (evt) { evt.preventDefault(); tis.move(evt); }); };構造函數和事件處理程序:
Swiper
陣列跟踪手指位置。 >初始化起始位置,並且(使用
防止默認滾動行為)更新位置並調用回調。
Swiper
Swiper.prototype.diff = function () { return this.points[1] - this.points[0]; }; Swiper.prototype.move = function (evt) { // Logic to update this.points[1] based on evt.targetTouches this.cbs.swiping(this.diff()); this.points[0] = this.points[1]; }; Swiper.prototype.start = function(evt) { // Logic to update this.points[0] based on evt.targetTouches this.points[1] = this.points[0]; };>
>計算差異和處理運動的方法:points
touchstart
touchmove
>計算滑動距離。 preventDefault
>更新位置,以距離調用回調,並更新先前的位置以進行準確跟踪。
>屬性的完整實現對於簡短而言是至關重要的,但對於完整的功能至關重要。)
插件調用:
>如何使用插件的示例:
var target = $('#target'), pictures = $('.pictures', target), MAX_LEFT = -990, MAX_RIGHT = 0, currPos = 0, cb = { swiping: function (displacement) { currPos += displacement; currPos = Math.max(MAX_LEFT, Math.min(currPos, MAX_RIGHT)); pictures.css('left', currPos + 'px'); } }; target.swiper(cb);
>這可以設置旋轉木製,定義限制,並通過更新旋轉木馬位置的回調功能將插件綁定。 這種修訂後的響應提供了對插件創建的更簡潔,更有條理的解釋,重點關注關鍵方面,並為清晰度省略了較少的基本細節。 請記住,對於完整功能,必須完整實現diff()
和>確保旋轉旋轉木馬保持在邊界內。 Swiper
>類方法(尤其是在設備之間處理不同的觸摸事件屬性)。
以上是用於觸摸刷的jQuery插件 - 第1部分,共2部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具