jQuery實(shí)現(xiàn)的移動(dòng)端圖片縮放功能組件示例
本文實(shí)例講述了jQuery實(shí)現(xiàn)的移動(dòng)端圖片縮放功能組件。分享給大家供大家參考,具體如下:
源碼見(jiàn)這里:https://github.com/CaptainLiao/zujian/tree/master/matrix
HTML結(jié)構(gòu):
<div class="box"> <img src="d1981a8ba21975314fd2edf9c09447bb.jpg"> <img src="8e7075a2c6b7fb1e083914db000a70c5.jpg"> <img src="d1981a8ba21975314fd2edf9c09447bb.jpg"> </div>
JS:
function Matrix($el, options) {
this.$el = $el;
this.clientW = $(window).width();
this.imgLen = 0;
this.cur_x = 0;
this.start_x = 0;
// 差值
this.m = 0;
this.params = $.extend({},{plus: 1.1, reduce: .9}, options);
this.plus = this.params.plus;
this.reduce = this.params.reduce;
// 縮放初始值
this.s = 1;
this.flag = false;
}
Matrix.prototype.chooseImg = function () {
var _this = this,
$wrap = this.$el;
$wrap.on('click','img' ,function () {
var mask = $('<div class="fui-mask"></div>'),
fui_pop = $('<div class="fui-pop"></div>'),
fui_slider = $('<ul class="fui-slider"></ul>'),
index,
$imgs = $wrap.find('img'),
url = '',
li = '',
clientWidth = _this.clientW;
index = $(this).index();
$('body').append(mask).append(fui_pop);
$('.fui-pop').append(fui_slider);
if($imgs) {
$imgs.each(function () {
url = $(this).attr('src');
li += ' <li class="fui-slider-item" style="width: '+clientWidth+'px"><img src='+url+' alt=""></li>';
_this.imgLen++;
});
$('.fui-slider').append(li)
.width(_this.imgLen * 100 +'%')
.css('left', -index * clientWidth +'px');
}else {
alert('請(qǐng)選擇圖片@@');
return
}
_this.touchSlide();
_this.closeImg();
_this.scale();
})
};
Matrix.prototype.closeImg = function () {
var _this = this;
$('.fui-slider-item').on('click', function (e) {
$('.fui-pop, .fui-mask').remove();
_this.imgLen = 0;
})
};
Matrix.prototype.touchSlide = function () {
var _this = this,
$slider = $('.fui-slider'),
clientW = this.clientW,
imgLen = this.imgLen;
$slider.on('touchstart', '.fui-slider-item', function (e) {
var index = $(this).index(),
m = _this.m,
left = parseInt($slider.css('left'));
_this.start_x = e.originalEvent.touches[0].clientX;
if(_this.flag) return;
_this.flag = true;
$(this).on('touchmove', function (e) {
_this.cur_x = e.originalEvent.touches[0].clientX;
m = _this.cur_x - _this.start_x;
if(Math.abs(m) > 50) {
console.log(m);
console.log(m);
$slider
.css('left', left+m+'px')
.find('.fui-slider-item').css({
'transform': 'scale(1)',
'transform-origin': '0% 0%'
});
}
});
$(this).on('touchend', function (e) {
_this.flag = false;
// 每次touchend的時(shí)候,將縮放值初始化
_this.s = 1;
if(Math.abs(m) < clientW / 3){
$slider.css('left', left+'px');
return;
}
index = index % imgLen;
if(index ==0){
if(m > 0) {
$slider.css('left', left+'px');
}else {
$slider.css('left', left-clientW+'px');
}
}else if(index < imgLen-1){
if(m > 0) {
$slider.css('left', left+clientW+'px');
}else {
$slider.css('left', left-clientW+'px');
}
}else{
if(m < 0) {
$slider.css('left', left+'px');
}else {
$slider.css('left', left+clientW+'px');
}
}
});
})
};
Matrix.prototype.scale = function() {
var _this = this;
$('.fui-slider').on('mousewheel','.fui-slider-item', function(e) {
var
oEvent = e.originalEvent,
p_x = 0,
p_y = 0,
delta = oEvent.wheelDelta || -oEvent.delta;
if(delta > 0) {
_this.s *= _this.plus;
}else {
_this.s *= _this.reduce;
}
p_x = (oEvent.clientX / $(window).width()) * 100;
p_y = (oEvent.clientY / $(window).height()) *100 ;
$(this).css({
'transform': 'scale('+_this.s+')',
'transform-origin':p_x+'% '+p_y+'%',
'-webkit-transform-origin': p_x+'% '+p_y+'%'
});
})
};
$.fn.zoom = function (options, cb) {
var zoom = new Matrix(this, options, cb);
return zoom.chooseImg();
};
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery切換特效與技巧總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery實(shí)現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁(yè)面布局
- jquery 圖片預(yù)加載 自動(dòng)等比例縮放插件
- 基于JQuery實(shí)現(xiàn)的圖片自動(dòng)進(jìn)行縮放和裁剪處理
- jquery 圖片縮放拖動(dòng)的簡(jiǎn)單實(shí)例
- jquery實(shí)現(xiàn)圖片按比例縮放示例
- 基于jquery實(shí)現(xiàn)圖片相關(guān)操作(重繪、獲取尺寸、調(diào)整大小、縮放)
- jQuery實(shí)現(xiàn)的鼠標(biāo)滾輪控制圖片縮放功能實(shí)例
- jQuery圖片縮放插件smartZoom使用實(shí)例詳解
- JQuery 圖片延遲加載并等比縮放插件
- jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼
- 基于jquery的防止大圖片撐破頁(yè)面的實(shí)現(xiàn)代碼(立即縮放)
相關(guān)文章
jquery實(shí)現(xiàn)漂亮的二級(jí)下拉菜單代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)漂亮的二級(jí)下拉菜單代碼,涉及jquery鼠標(biāo)click事件控制頁(yè)面class屬性動(dòng)態(tài)變換效果的切換技巧,非常美觀實(shí)用,需要的朋友可以參考下2015-08-08
jQuery簡(jiǎn)單實(shí)現(xiàn)驗(yàn)證郵箱格式
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)驗(yàn)證郵箱格式的相關(guān)資料,需要的朋友可以參考下2015-07-07
jQuery 綁定事件到動(dòng)態(tài)創(chuàng)建的元素上的方法實(shí)例
這篇文章介紹了jQuery 綁定事件到動(dòng)態(tài)創(chuàng)建的元素上的方法實(shí)例,有需要的朋友可以參考一下2013-08-08
jquery分頁(yè)插件AmSetPager(自寫(xiě))
第一次做的插件,我這個(gè)插件好像使用有些另類(lèi),是調(diào)用數(shù)據(jù)展示容器的元素$(#DataContent).AmSetPager({...});在參數(shù)中配置分頁(yè)容器元素ID。寫(xiě)完插件后看別的插件都是調(diào)用分頁(yè)元素ID2013-04-04
jQuery實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)顯示動(dòng)畫(huà)邊框特效
本文主要介紹了jQuery鼠標(biāo)經(jīng)過(guò)顯示動(dòng)畫(huà)邊框特效的實(shí)例代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
jquery ajax的success回調(diào)函數(shù)中實(shí)現(xiàn)按鈕置灰倒計(jì)時(shí)
實(shí)現(xiàn)異步手機(jī)發(fā)送短信成功之后在ajax的success回調(diào)中實(shí)現(xiàn)將發(fā)送按鈕置灰并倒計(jì)時(shí),具體的實(shí)現(xiàn)示例如下,喜歡的朋友可以研究下2013-11-11

