-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.bay-window.min.js
8 lines (8 loc) · 4.11 KB
/
jquery.bay-window.min.js
1
2
3
4
5
6
7
8
/*!
* jquery.bay-window 0.0.5 (https://github.com/trague/jquery.bay-window)
* Author: Zhao WenZheng
* Contact: [email protected]
* Copyright 2022 jquery.bay-window
* Licensed under MIT (https://github.com/trague/jquery.bay-window/blob/main/LICENSE)
*/
!function(i){"function"==typeof define&&define.amd?define(["jquery"],i):"object"==typeof module&&module.exports?module.exports=function(t,e){return void 0===e&&(e="undefined"!=typeof window?require("jquery"):require("jquery")(t)),i(e),e}:i(jQuery)}(function(s){Date.now||(Date.now=function(){return(new Date).getTime()});for(var o,t=["webkit","moz"],e=0;e<t.length&&!window.requestAnimationFrame;++e){var i=t[e];window.requestAnimationFrame=window[i+"RequestAnimationFrame"],window.cancelAnimationFrame=window[i+"CancelAnimationFrame"]||window[i+"CancelRequestAnimationFrame"]}!/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent)&&window.requestAnimationFrame&&window.cancelAnimationFrame||(o=0,window.requestAnimationFrame=function(t){var e=Date.now(),i=Math.max(o+16,e);return setTimeout(function(){t(o=i)},i-e)},window.cancelAnimationFrame=clearTimeout);function a(t,e){this.$element=s(t),this.options=s.extend({},a.DEFAULTS,e),this.isToLeft=!1,this.isToTop=!1,this.cancelId,this.top=this.options.startTop,this.left=this.options.startLeft,this.lastTime=0,this.$element.css({position:"fixed",top:n?0:this.top,left:n?0:this.left,cursor:"pointer",transform:n?"translate("+this.left+"px,"+this.top+"px)":"",zIndex:this.options.zIndex}),this.$element.hover(this.stop.bind(this),this.run.bind(this))}var d="bayWindow",n="transform"in document.documentElement.style;s.extend({createBayWindowElement:function(t){s("#"+(i="createBayWindowElementStyleTag")).length||(i=['<style id="'+i+'">'," .default-bay-window { position: fixed; }"," .default-bay-window__img-wrap { display: block; }"," .default-bay-window__img { display: block; height: 100%; width: 100%; object-fit: cover; }"," .default-bay-window__close { -webkit-box-shadow: none; box-shadow: none; outline: none; cursor: pointer; }"," .default-bay-window__close { display: block; margin-left: auto; background: #fff; color: #333; border: 1px solid #999; }"," .default-bay-window__close:hover { background: #eee; }","</style>"].join(""),s("head").append(i));var e,i={id:"jsBayWindow",height:"130px",width:"230px",src:"",href:"",target:"__blank",closeText:"关闭"};if(s.extend(i,t||{}),!s("#"+i.id).length)return t=['<div id="'+i.id+'" class="default-bay-window">',' <a class="default-bay-window__img-wrap" href="'+i.href+'" style="'+("height: "+i.height+"; width: "+i.width+";")+'" target="'+i.target+'">',' <img class="default-bay-window__img" src="'+i.src+'" />'," </a>",' <button class="jsBayWindowClose default-bay-window__close" type="button">'+i.closeText+"</button>","</div>"].join(""),e=s(t),s("body").append(e),e.find(".jsBayWindowClose").on("click",function(){var t=e.data(d);t&&(s(this).off("click"),t.destroy())}),e}});a.DEFAULTS={stepLength:1,startTop:0,startLeft:0,zIndex:9999},a.prototype._nextStep=function(t){t=t||1;var e=s(window).width()-this.$element.width()-10,i=s(window).height()-this.$element.height()-10;this.left+=(this.isToLeft?-1:1)*t,this.left>=e?(this.left=e,this.isToLeft=!0):this.left<=0&&(this.left=0,this.isToLeft=!1),this.top+=(this.isToTop?-1:1)*t,this.top>=i?(this.top=i,this.isToTop=!0):this.top<=0&&(this.top=0,this.isToTop=!1),n?this.$element.css({transform:"translate("+this.left+"px,"+this.top+"px)"}):this.$element.css({left:this.left,top:this.top})},a.prototype.run=function(t){this.stop();var e=this.options.stepLength,i=t-this.lastTime;this.lastTime=t,i&&i<16&&(e=(i/16.7*e).toFixed(2)),this._nextStep(e),this.cancelId=requestAnimationFrame(this.run.bind(this))},a.prototype.stop=function(){cancelAnimationFrame(this.cancelId)},a.prototype.destroy=function(){this.stop(),this.$element.remove()},s.fn.bayWindow=function(n){return n=n||{},this&&this.length||console.warn("Bay Window no matching elements"),this.each(function(){var t=s(this),e=t.data(d),i="object"==typeof n?n:{},o="string"==typeof n?n:"run";e&&"object"!=typeof n||(e&&e.stop(),e=new a(this,i),t.data(d,e)),e[o]?e[o]():console.warn('"'+o+'"',"is not bay window method.")})}});