>本文详细介绍了构建用于检测触摸设备上水平滑动的jQuery插件。 第一部分着重于创建响应式图像旋转木马。第二部分(此处不包括)会添加滑动检测。
>密钥概念:
Swiper
>教程解释了插件实现,包括设置轮播限制,跟踪位置以及使用JSON。
相关的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/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791350855.jpg" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791497040.jpg" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791447095.jpg" class="lazy" alt="A jQuery Plugin for Touch Swiping - Part 1 of 2 " /></div> <div class="pic"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174035791526338.jpg" 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));
.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中文网其他相关文章!