夏邑12345_前端

floatingcarousel.min.js 7.9KB

123456789101112131415161718192021222324
  1. /*
  2. * floatingCarousel 4.0
  3. * Copyright (c) 2017 Adam Lafene
  4. *
  5. * Licensed under the terms of the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. */
  9. (function(q,z){"function"===typeof define&&define.amd?define([],z(q)):"object"===typeof exports?module.exports=z(q):q.floatingCarousel=z(q)})("undefined"!==typeof global?global:this.window||this.global,function(q){var z=!!document.querySelector&&!!q.addEventListener,H=window.jQuery,u,W={autoScroll:!1,autoScrollDirection:"left",autoScrollSpeed:1E4,initClass:"floatingCarouselContainer",intervalRate:16,looped:!0,scrollerAlignment:"horizontal",scrollerOffset:0,scrollSpeed:"medium",beforeCreateFunction:null,
  10. afterCreateFunction:null,enableTouchEvents:!0,touchOverflowHidden:!0,reverseOnTouch:!0},r=function(c,l,f){if("[object Object]"===Object.prototype.toString.call(c))for(var a in c)Object.prototype.hasOwnProperty.call(c,a)&&l.call(f,c[a],a,c);else{a=0;for(var X=c.length;a<X;a++)l.call(f,c[a],a,c)}},Y=function(c,l){var f={};r(c,function(a,l){f[l]=c[l]});r(l,function(a,c){f[c]=l[c]});return f},A=function(c,l){if(c.length)r(c,function(a){for(var c in l)a.style[c]=l[c]});else for(var f in l)c.style[f]=l[f];
  11. return c},M=function(c,l){var f=Element.prototype;return(f.matches||f.webkitMatchesSelector||f.mozMatchesSelector||f.msMatchesSelector||function(a){return-1!==[].indexOf.call(document.querySelectorAll(a),this)}).call(c,l)},D=function(c,l){return Array.prototype.filter.call(c.children,function(c){return M(c,l)})},N=function(c){c=c.getBoundingClientRect();var l=window.pageYOffset||document.documentElement.scrollTop,f=window.pageXOffset||document.documentElement.scrollLeft;return{top:parseInt(c.top)+
  12. l,left:parseInt(c.left)+f}},I=function(c,l){var f={},a=c||[];"string"==typeof a&&(a=document.querySelectorAll(a));if(1<a.length){var q=[];r(a,function(b){1===b.nodeType&&q.push(I(b,l))});return q}a=a[0]||a;var k=0,y=!1,h=0,J=0,E=0,v,g,p,F,B,n,e,d,w,C,x,m,t,b,G=function(a,d){return"left"==a?y&&b.reverseOnTouch?d>t:d<t:y&&b.reverseOnTouch?d<t:d>t},K=function(){0!=b.looped&&(p&&(clearInterval(p),p=0),d=d?d:"vertical"!=b.scrollerAlignment.toLowerCase()?parseInt(e[0].style.left):parseInt(e[0].style.top),
  13. b.autoScrollSpeed=1E3>b.autoScrollSpeed?1E3:b.autoScrollSpeed,b.autoScrollSpeed/v<n?m=Math.round(n/(b.autoScrollSpeed/v)):(m=1,v=Math.round(b.autoScrollSpeed/n)),p=setInterval(function(){switch(b.autoScrollDirection.toLowerCase()){case "right":case "down":d+m>h?(d=0,k=0==k?1:0):d+=m;break;default:d-m<0-(h-n)?(d=n,k=0==k?1:0):d-=m}"vertical"!=b.scrollerAlignment.toLowerCase()?(e[k].style.left=d+"px",e[0==k?1:0].style.left=d-h+"px"):(e[k].style.top=d+"px",e[0==k?1:0].style.top=d-h+"px")},v))},O=function(){p&&
  14. (clearInterval(p),p=0);t="vertical"!=b.scrollerAlignment.toLowerCase()?Math.round(N(g).left+g.offsetWidth/2):Math.round(N(g).top+g.offsetHeight/2);d=d?d:"vertical"!=b.scrollerAlignment.toLowerCase()?parseInt(e[0].style.left):parseInt(e[0].style.top);u=setInterval(function(){try{var a="vertical"!=b.scrollerAlignment.toLowerCase()?B.x:B.y}catch(Z){return}var c=n/2;var f=a<t?t-a:a-t;m=f<Math.ceil(c/100*30)?1:f<Math.ceil(c/100*50)?2*w:f<Math.ceil(c/100*70)?3*w:f<Math.ceil(c/100*90)?4*w:6*w;G("left",a)?
  15. 0<d+m&&0==b.looped?d=0:d+m>h?(d=0,k=0==k?1:0):d+=m:G("right",a)&&(d-m<0-(h-n)?0==b.looped?d=0-(h-n):(d=n,k=0==k?1:0):d-=m);"vertical"!=b.scrollerAlignment.toLowerCase()?(e[k].style.left=d+"px",e[0==k?1:0].style.left=d-h+"px"):(e[k].style.top=d+"px",e[0==k?1:0].style.top=d-h+"px")},v)},L=function(a){if(u&&(clearInterval(u),u=0,a&&0!=b.looped&&1!=b.autoScroll&&1<m)){a=0;for(var c=m;1<c;c--)a+=c;var f="vertical"!=b.scrollerAlignment.toLowerCase()?B.x:B.y;G("left",f)?d+a>h&&(d-=h,k=0==k?1:0):d-a<0-(h-
  16. n)&&(d+=h,k=0==k?1:0);var g=setInterval(function(){1<m?(d=G("left",f)?d+m:d-m,"vertical"!=b.scrollerAlignment.toLowerCase()?(e[k].style.left=d+"px",e[0==k?1:0].style.left=d-h+"px"):(e[k].style.top=d+"px",e[0==k?1:0].style.top=d-h+"px"),m--):clearInterval(g)},v)}},P=function(a){var b={x:0,y:0};if(a.changedTouches)a=a.changedTouches[0],b.x=a.pageX,b.y=a.pageY;else if(a.pageX||a.pageY)b.x=a.pageX,b.y=a.pageY;else{var c=document.documentElement,d=document.body;b.x=a.clientX+((c.scrollLeft||d.scrollLeft)-
  17. (c.clientLeft||0));b.y=a.clientY+((c.scrollTop||d.scrollTop)-(c.clientTop||0))}B=b},Q=function(){y=!0;b.touchOverflowHidden&&(document.body.style.overflow="hidden");O()},R=function(){b.touchOverflowHidden&&(document.body.style.overflow="");L(!0);1==b.autoScroll&&K()},S=function(a){P(a)},T=function(){y||O()},U=function(){y||(L(!0),1==b.autoScroll&&K())},V=function(a){y||P(a)};f.destroy=function(){b&&(f.pause(),1<a.children.length&&e[1].parentNode.removeChild(e[1]),r(e[0].children,function(a){a.removeAttribute("style")}),
  18. M(e[0],"div")?a.innerHTML=e[0].innerHTML:e[0].removeAttribute("style"),a.removeAttribute("style"),a.classList.remove(b.initClass),d=b=null,h=0)};f.update=function(a){f.destroy();f.init(a)};f.play=function(){F||(b.enableTouchEvents&&(a.addEventListener("touchstart",Q),a.addEventListener("touchend",R),a.addEventListener("touchmove",S)),a.addEventListener("mouseenter",T),a.addEventListener("mouseleave",U),a.addEventListener("mousemove",V),!0===b.autoScroll&&K(),F=!0)};f.pause=function(){F&&(L(),p&&(clearInterval(p),
  19. p=0),u&&(clearInterval(u),u=0),b.enableTouchEvents&&(a.removeEventListener("touchstart",Q),a.removeEventListener("touchend",R),a.removeEventListener("touchmove",S)),a.removeEventListener("mouseenter",T),a.removeEventListener("mouseleave",U),a.removeEventListener("mousemove",V),F=!1)};f.init=function(c){if(1>a.length)return!1;g=a;if(z){b=Y(W,c||{});null!=b.beforeCreateFunction&&"function"===typeof b.beforeCreateFunction&&b.beforeCreateFunction.call(a);a.classList.add(b.initClass);v=b.intervalRate;
  20. g.style.paddingLeft="0";g.style.paddingRight="0";n=g.offsetWidth;switch(a.children[0].nodeName.toLowerCase()){case "div":g.innerHTML="<div>"+g.innerHTML+"</div>";g.innerHTML+=g.innerHTML;e=D(a,"div");C=D(e[0],"div");break;case "ul":g.innerHTML+=g.innerHTML;e=a.querySelectorAll("ul");C=D(e[0],"li");break;case "ol":g.innerHTML+=g.innerHTML;e=a.querySelectorAll("ol");C=D(e[0],"li");break;default:return console.log("unable to initialise scroller - please ensure contents are either in a UL, an OL or in DIVs"),
  21. !1}A(C,{"float":"left"});switch(b.scrollSpeed.toLowerCase()){case "slow":w=1;break;case "fast":w=4;break;default:w=2}r(C,function(a){var c=parseInt(window.getComputedStyle(a).getPropertyValue("margin-top")),d=parseInt(window.getComputedStyle(a).getPropertyValue("margin-bottom")),e=parseInt(window.getComputedStyle(a).getPropertyValue("margin-left")),f=parseInt(window.getComputedStyle(a).getPropertyValue("margin-right"));c=a.offsetHeight+c+d;e=a.offsetWidth+e+f;"vertical"!=b.scrollerAlignment.toLowerCase()?
  22. (h+=e,a.offsetHeight>J&&(J=c)):(h+=c,a.offsetWidth>E&&(E=e))});"vertical"!=b.scrollerAlignment.toLowerCase()?g.style.height=J+"px":(g.style.width=E+"px",g.style.height=0<g.offsetHeight?g.offsetHeight+"px":g.parentNode.offsetHeight+"px",n=g.offsetHeight);h>n?(x=Math.round(h/100*b.scrollerOffset)-Math.round(n/2),x>h-n&&(x=h-n),g.style.overflow="hidden",g.style.position="relative",r(e,function(a){a.style.position="absolute";"vertical"!=b.scrollerAlignment.toLowerCase()?A(a,{top:0,width:h+"px"}):A(a,
  23. {left:0,height:h+"px",width:E+"px"});r(a.children,function(a){a.style["float"]="left";a.style.position="static"})}),"vertical"!=b.scrollerAlignment.toLowerCase()?(e[0].style.left=0<x?"-"+x+"px":"0",1==b.looped?e[1].style.left=e[0].offsetLeft-h+"px":A(e[1],{display:"none",top:"-1000px"})):(e[0].style.top=0<x?"-"+x+"px":"0",1==b.looped?e[1].style.top=e[0].offsetTop-h+"px":A(e[1],{display:"none",top:"-1000px"})),f.play(),null!=b.afterCreateFunction&&"function"===typeof b.afterCreateFunction&&b.afterCreateFunction.call(a)):
  24. (e[1].parentNode.removeChild(e[1]),null!=b.afterCreateFunction&&H.isFunction(b.afterCreateFunction)&&b.afterCreateFunction.call(a))}};f.init(l);return f};H&&(H.fn.floatingCarousel=function(c){return new I(this,c)});return I});