-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
js全局变量 与 const相关, js插件写法 #39
Comments
// js 插件写法
;(function(undefined) {
"use strict"
var _global;
var plugin = {
add: function(n1,n2){ return n1 + n2; },//加
sub: function(n1,n2){ return n1 - n2; },//减
mul: function(n1,n2){ return n1 * n2; },//乘
div: function(n1,n2){ return n1 / n2; },//除
sur: function(n1,n2){ return n1 % n2; } //余
}
// 最后将插件对象暴露给全局对象
_global = (function(){ return this || (0, eval)('this'); }());
if (typeof module !== "undefined" && module.exports) {
module.exports = plugin;
} else if (typeof define === "function" && define.amd) {
define(function(){return plugin;});
} else {
!('plugin' in _global) && (_global.plugin = plugin);
}
}()); |
// 例如一个依赖jq的 滑动拼图验证
;(function(undefined) {
'use strict'
var _global
var SlideImageVerify = function(ele, opt) {
this.$ele = $(ele)
this.defaults = {
oriImg: '',
tarImg: '',
coordinate_y: 0,
verifyCB: function() {}
}
this.settings = $.extend({}, this.defaults, opt)
// this.oriImg
// this.tarImg
this.coordinate_y
this.dragTimerState = false
this.touchX = 0
this.disTouchX = 0
this.pending = false
this.init()
}
SlideImageVerify.prototype = {
constructor: this,
init: function() {
this.initDom()
this._touchstart()
this._touchend()
},
initDom: function() {
var slideDom = $(
'<div class="SIV_box">' +
'<div class="oriImg">' +
'<div class="tarImg"></div>' +
'</div>' +
'<div class="processBar">' +
'<div class="tip">请滑动左边滑块完成拼图</div>' +
'<div class="slideBtn"></div>' +
'</div>' +
'</div>'
)
this.$ele.append(slideDom)
this.$oriImg = $('.SIV_box .oriImg')
this.$tarImg = $('.SIV_box .tarImg')
this.$slideBtn = $('.SIV_box .slideBtn')
this.$tip = $('.SIV_box .tip')
this.radio = this.$oriImg.outerWidth() / 600
this.$oriImg.css({ 'background-image': 'url(' + this.settings.oriImg + ')' })
this.$tarImg.css({ 'background-image': 'url(' + this.settings.tarImg + ')', top: this.settings.coordinate_y * this.radio })
},
_touchstart: function() {
var _this = this
_this.$slideBtn.on('touchstart', function(e) {
_this.$slideBtn.css('pointer-events', 'none')
setTimeout(function() {
_this.$slideBtn.css('pointer-events', 'all')
}, 400)
if (_this.dragTimerState || _this.$slideBtn.is(':animated')) {
_this.touchX = 0
return false
}
if (_this.$slideBtn.css('left') == '0px') {
_this.touchX = e.originalEvent.targetTouches[0].pageX
//
_this._touchmove()
}
})
},
_touchmove: function() {
var _this = this
_this.$slideBtn.on('touchmove', function(e) {
e.preventDefault()
if (_this.dragTimerState || _this.$slideBtn.is(':animated')) {
return false
} else {
var curX = e.originalEvent.targetTouches[0].pageX - _this.touchX
if (curX != 0) {
if (curX < 0) {
curX = 0
} else if (curX >= 270) {
// 300 - 30
curX = 270
}
_this.disTouchX = curX
_this.$slideBtn.css({ left: curX })
_this.$tarImg.css({ left: curX })
}
}
})
},
_touchend: function() {
var _this = this
_this.$slideBtn.on('touchend', function() {
_this.$slideBtn.off('touchmove')
// 校验
// resetSlide()
_this.settings.verifyCB && !_this.pending && _this.settings.verifyCB(parseInt(_this.disTouchX / _this.radio));
_this.pending = true
})
},
resetSlide: function() {
var _this = this
_this.$slideBtn.animate({ left: 0 }, 50,function(){
_this.pending = false
_this.$tip.text('请重新滑动左边滑块')
})
_this.$tarImg.animate({ left: 0 }, 50)
},
refreshSlide: function(opt) {
this.resetSlide()
this.settings = $.extend({}, this.settings, opt)
// this.$ele.empty()
// this.init()
this.$oriImg.css({ 'background-image': 'url(' + this.settings.oriImg + ')' })
this.$tarImg.css({ 'background-image': 'url(' + this.settings.tarImg + ')', top: this.settings.coordinate_y * this.radio })
}
}
var inlineCss =
".SIV_box *, .SIV_box { margin: 0; padding: 0; box-sizing: border-box; } .SIV_box { padding: 10px 10px 0; /* width: 80%; */ } .SIV_box .oriImg { position: relative; width: 300px; /* height: 150px; */ padding-top: 50%; background-repeat: no-repeat; background-size: contain; background-position: center center; } .SIV_box .tarImg { position: absolute; width: 10%; padding-top: 8%; top: 0; left: 0; background-repeat: no-repeat; background-size: contain; background-position: center center; z-index: 1; } .SIV_box .processBar { position: relative; height: 20px; background-color: #dfe1e2; border-radius: 15px; box-shadow: inset 0 0 5px #ccc; margin: 15px 0; } .SIV_box .slideBtn { position: absolute; z-index: 1; left: 0; top: -5px; height: 30px; width: 30px; border-radius: 50%; background-repeat: no-repeat; background-size: 32px 32px; background-position: -2px -1px; background-image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAlCAYAAAAuqZsAAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyFpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTQyIDc5LjE2MDkyNCwgMjAxNy8wNy8xMy0wMTowNjozOSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIChXaW5kb3dzKSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDozQkYxRDU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rrG4tem6lLrVqIZiKuijC2TDWJJusaEywrfDFgu1rYSKDZdmXTeXtoR/LZQIZb5GPPyiQ4Vk2qhODWWAAyJQzPl/ndBbKF/YgnAfry688Q0CTpVAIX+iSUAo+SAFOu0cgTFWep75dcvhRzdNCl3NtVXRX3uzzF/Ie0aFGowRJIR7ObYo3YdvXGJ0/NrmklzCW1iLrlt+P6K065BoR57dPE6FENyUh8JGqYrpTx3pVQOxqcmi+piG6ed2w/Dp5TnCbHP0ycko1wtqnnMtRp/2nscEH1zlojvQcz9TBLzkUVH6V7z92SYoE+uVkPCyzOlTGZFr1xjtDruzJOQhUKxnpXzxNfXTmJqD4a7gQ3GppJJMb7Ka1BqJk4P1NJ3Hc41zjSFwoF58Y8IOByk1oLV5CabQuG2U8zZoJqej1HM0TGU9kEpg1r+F90t/BjKdE6Gb3I+MVwKFMIe2JaDu2bHnSEk5QukZi0u8ZUgYdp61OlyHutSXIGo/BNgAC8vQDkjVFkBAAAAAElFTkSuQmCC'); border: 1px solid #ccc; } .SIV_box .tip { text-align: center; font-size: 12px; line-height: 20px; color: #333; }"
var styleObj = $('<style type="text/css">' + inlineCss + '</style>')
$('head').prepend(styleObj)
_global = (function() {
return this || (0, eval)('this')
})()
if (typeof module !== 'undefined' && module.exports) {
module.exports = SlideImageVerify
} else if (typeof define === 'function' && define.amd) {
define(function() {
return SlideImageVerify
})
} else {
!('SlideImageVerify' in _global) && (_global.SlideImageVerify = SlideImageVerify)
}
})()
// 使用时
new SlideImageVerify('#slideImageBox', {
oriImg: oriImg, // 整张背景的 图片
tarImg: tarImg, // 拼图图块的 图片
coordinate_y: coordinate_y, // 拼图图块 垂直距离
verifyCB: function(res) {} // 回调
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
全局变量的优点:
全局变量的缺点:
**let命令、const命令、class命令声明的全局变量,不属于全局对象的属性。**也就是说,从ES6开始,全局变量将逐步与全局对象的属性脱钩。
项目中留意到,注意:
The text was updated successfully, but these errors were encountered: