main.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. var alan = (function(document, undefined) {
  2. var readyRE = /complete|loaded|interactive/;
  3. var idSelectorRE = /^#([\w-]+)$/;
  4. var classSelectorRE = /^\.([\w-]+)$/;
  5. var tagSelectorRE = /^[\w-]+$/;
  6. var translateRE = /translate(?:3d)?\((.+?)\)/;
  7. var translateMatrixRE = /matrix(3d)?\((.+?)\)/;
  8. var $ = function(selector, context) {
  9. context = context || document;
  10. if (!selector)
  11. return wrap();
  12. if (typeof selector === 'object')
  13. if ($.isArrayLike(selector)) {
  14. return wrap($.slice.call(selector), null);
  15. } else {
  16. return wrap([selector], null);
  17. }
  18. if (typeof selector === 'function')
  19. return $.ready(selector);
  20. if (typeof selector === 'string') {
  21. try {
  22. selector = selector.trim();
  23. if (idSelectorRE.test(selector)) {
  24. var found = document.getElementById(RegExp.$1);
  25. return wrap(found ? [found] : []);
  26. }
  27. return wrap($.qsa(selector, context), selector);
  28. } catch (e) {}
  29. }
  30. return wrap();
  31. };
  32. var wrap = function(dom, selector) {
  33. dom = dom || [];
  34. Object.setPrototypeOf(dom, $.fn);
  35. dom.selector = selector || '';
  36. return dom;
  37. };
  38. /**
  39. * querySelectorAll
  40. * @param {type} selector
  41. * @param {type} context
  42. * @returns {Array}
  43. */
  44. $.qsa = function(selector, context) {
  45. context = context || document;
  46. return $.slice.call(classSelectorRE.test(selector) ? context.getElementsByClassName(RegExp.$1) : tagSelectorRE.test(selector) ? context.getElementsByTagName(selector) : context.querySelectorAll(selector));
  47. };
  48. $.uuid = 0;
  49. $.data = {};
  50. $.insertAfter = function(elem,tarElem){
  51. var parent = tarElem.parentNode;
  52. if (parent.lastChlid == tarElem) {
  53. parent.appendChild(elem);
  54. }else{
  55. parent.insertBefore(elem,tarElem.nextSibling);
  56. }
  57. };
  58. // 查找兄弟元素
  59. $.getSiblings = function(o){
  60. var a = [];
  61. var p = o.previousSibling;
  62. while(p){
  63. if(p.nodeType === 1){
  64. a.push(p);
  65. }
  66. p = p.previousSibling;
  67. }
  68. a.reverse();
  69. var n = o.nextSibling;
  70. while(n){
  71. if(n.nodeType === 1){
  72. a.push(n);
  73. }
  74. n = n.nextSibling;
  75. }
  76. return a;
  77. };
  78. $.toggleClass = function( elem, c ) {
  79. if(elem.classList.contains(c)){
  80. elem.classList.remove(c);
  81. }else{
  82. elem.classList.add(c);
  83. }
  84. };
  85. /* 移动端点击事件模拟PC端hover事件
  86. * class1,选择匹配的元素
  87. * class2,匹配元素里的a元素,阻止它跳转
  88. * */
  89. $.touchToHover = function (class1,class2) {
  90. [].slice.call(document.querySelectorAll(class1)).forEach( function( el) {
  91. el.querySelector(class2).addEventListener( 'touchstart', function(e) {
  92. console.log(this);
  93. //e.stopPropagation();
  94. e.preventDefault();
  95. }, false );
  96. el.addEventListener( 'touchstart', function(e) {
  97. $.toggleClass( this, 'cs-hover' );
  98. }, false );
  99. });
  100. };
  101. $.getStyle = function(elem,attr){
  102. return elem.currentStyle ? elem.currentStyle[attr] : window.getComputedStyle(elem,false)[attr];
  103. };
  104. $.getElemPosition = function(elem){
  105. var oPositon = elem.getBoundingClientRect();
  106. return {
  107. top:oPositon.top,
  108. bottom:oPositon.bottom,
  109. left:oPositon.left,
  110. right:oPositon.right,
  111. };
  112. };
  113. $.Event = {
  114. on:function(elem,type,handler){
  115. if (elem.addEventListener) {
  116. elem.addEventListener(type,handler,false);
  117. }else if(elem.attachEvent){
  118. elem.attachEvent('on'+type,handler);
  119. }else{
  120. elem['on'+type] = handler;
  121. }
  122. },
  123. off:function(elem,type,handler){
  124. if (elem.removeEventListener) {
  125. elem.removeEventListener(type,handler,false);
  126. }else if (elem.detachEvent) {
  127. elem.detachEvent('on'+type,handler);
  128. }else{
  129. elem['on'+type] = null;
  130. }
  131. },
  132. getEvent:function(event){ //将它放在事件处理程序的开头,可以确保获取事件。
  133. return event ? event : window.event;
  134. },
  135. getTarget:function(event){ //目标元素
  136. return event.target || event.srcElement;
  137. },
  138. preventDefault:function(event){ //取消默认
  139. if (event.preventDefault) {
  140. event.preventDefault();
  141. }else{
  142. event.returnValue = false;
  143. }
  144. },
  145. stopPropagation:function(event){ //阻止冒泡
  146. if (event.stopPropagation) {
  147. event.stopPropagation();
  148. }else{
  149. event.cancelBubble = true;
  150. }
  151. },
  152. getRelatedTarget:function(event){//获得相关元素
  153. if (event.relatedTarget) {
  154. return event.relatedTarget;
  155. }else if (event.toElement) {
  156. return event.toElement;
  157. }else if (event.fromElement) {
  158. return event.fromElement;
  159. }else{
  160. return null;
  161. }
  162. },
  163. getButton:function(event){ //获取鼠标按钮
  164. if (alan.isSupported2) {
  165. return event.button;
  166. }else{
  167. switch(event.button){
  168. case 0:
  169. case 1:
  170. case 3:
  171. case 5:
  172. case 7:
  173. return 0;
  174. case 2:
  175. case 6:
  176. return 2;
  177. case 4:
  178. return 1;
  179. }
  180. }
  181. },
  182. getWheelDelta:function(event){ //滚轮的事件
  183. if (event.wheelDelta) {
  184. return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
  185. }else{
  186. return -event.detail * 40;
  187. }
  188. },
  189. getCharCode:function(event){
  190. if (typeof event.charCode == 'number') {
  191. return event.charCode;
  192. }else{
  193. return event.keyCode;
  194. }
  195. }
  196. };
  197. /**
  198. * 在连续整数中取得一个随机数
  199. * @param {number}
  200. * @param {number}
  201. * @param {string} 'star' || 'end' || 'both' 随机数包括starNum || endNum || both
  202. * @return 一个随机数
  203. */
  204. $.mathRandom = function(starNum,endNum,type){
  205. var num = endNum - starNum;
  206. switch (type) {
  207. case 'star' : return parseInt(Math.random()*num + starNum,10);
  208. case 'end' : return Math.floor(Math.random()*num + starNum) + 1;
  209. case 'both' : return Math.round(Math.random()*num + starNum);
  210. default : console.log('没有指定随机数的范围');
  211. }
  212. };
  213. // 获得数组中最小值
  214. $.getArrayMin = function(array) {
  215. /*var min = array[0];
  216. array.forEach(function (item) {
  217. if(item < min){
  218. min = item;
  219. }
  220. });
  221. return min;*/
  222. return Math.min.apply(Math,array);
  223. };
  224. // 获得数组中最大值
  225. $.getArrayMax = function (array) {
  226. return Math.max.apply(Math,array);
  227. };
  228. // 数组去重复
  229. $.getArrayNoRepeat = function (array) {
  230. var noRepeat = [];
  231. var data = {};
  232. array.forEach(function (item) {
  233. if(!data[item]){
  234. noRepeat.push(item);
  235. data[item] = true;
  236. }
  237. });
  238. return noRepeat;
  239. };
  240. $.isArray = function(val){
  241. return Array.isArray(val) || Object.prototype.toString.call(val) === '[object Array]';
  242. };
  243. $.isFunction = function(val){
  244. return Object.prototype.toString.call(val) == '[object Function]';
  245. };
  246. $.isRegExp = function(val){
  247. return Object.prototype.toString.call(val) == '[object RegExp]';
  248. };
  249. $.isMacWebkit = (navigator.userAgent.indexOf("Safari") !== -1 && navigator.userAgent.indexOf("Version") !== -1);
  250. $.isFirefox = (navigator.userAgent.indexOf("Firefox") !== -1);
  251. //
  252. $.fn = {
  253. each: function(callback) {
  254. [].every.call(this, function(el, idx) {
  255. return callback.call(el, idx, el) !== false;
  256. });
  257. return this;
  258. }
  259. };
  260. //兼容 AMD 模块
  261. if (typeof define === 'function' && define.amd) {
  262. define('alan', [], function() {
  263. return $;
  264. });
  265. }
  266. return $;
  267. })(document);
  268. // 滚动动画
  269. (function ($) {
  270. /*
  271. * 需要在css文件里添加 .scroll-animate.animated {visibility: hidden;} 样式,解决"闪一下"的bug
  272. * .disable-hover {pointer-events: none;} 也需要加在样式表中
  273. * 如果滚动事件失效,查看body元素是否设置了 {height:100%},它会影响滚动事件。
  274. * */
  275. var ScrollAnimate = function (opt) {
  276. this.opt = opt || {};
  277. this.className = this.opt.className || '.scroll-animate'; // 获取集合的class
  278. this.animateClass = this.opt.animateClass || 'animated'; // 动画依赖的class
  279. this.elem = document.querySelectorAll(this.className); // 需要滚动展示的元素
  280. this.position = []; // 所有元素的offsetTop距离数组
  281. this.windowHeight = ('innerHeight' in window) ? window.innerHeight : document.documentElement.clientHeight;
  282. this.time = null;
  283. this.body = document.body;
  284. this.init();
  285. };
  286. ScrollAnimate.prototype = {
  287. getPosition:function () {
  288. var self = this;
  289. self.position.length = 0; // 重置数组
  290. [].slice.call(self.elem).forEach(function (elem) {
  291. if(elem.classList.contains('father')){
  292. var children = elem.querySelectorAll(elem.getAttribute('data-child'));
  293. var delay = parseFloat(elem.getAttribute('data-delay'));
  294. [].slice.call(children).forEach(function (obj,index) {
  295. obj.classList.add('animated');
  296. obj.style.visibility = 'hidden';
  297. if(obj.getAttribute('data-delay')){
  298. obj.style.animationDelay = obj.getAttribute('data-delay') + 's';
  299. }else{
  300. obj.style.animationDelay = delay * index + 's';
  301. }
  302. })
  303. }else if(elem.classList.contains('font-fadeIn')){
  304. elem.style.visibility = 'hidden';
  305. }else{
  306. elem.classList.add('animated');
  307. }
  308. self.position.push(self.getOffsetTop(elem));
  309. });
  310. },
  311. getOffsetTop:function(element){
  312. var top;
  313. while (element.offsetTop === void 0) {
  314. element = element.parentNode;
  315. }
  316. top = element.offsetTop;
  317. while (element = element.offsetParent) {
  318. top += element.offsetTop;
  319. }
  320. return top;
  321. },
  322. scrollEvent:function () {
  323. var self = this;
  324. self.body.classList.add('disable-hover');
  325. clearTimeout(self.time);
  326. self.time = setTimeout(function () {
  327. self.body.classList.remove('disable-hover');
  328. },100);
  329. var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  330. self.position.forEach(function (item,index) {
  331. var currentElem = self.elem[index];
  332. var effect = currentElem.getAttribute('data-effect') || 'fadeInUp';
  333. var Tclass = currentElem.getAttribute('data-Tclass') || 'go';
  334. var flag = (scrollTop + self.windowHeight - 50 >item) ? true : false;
  335. // 判断当前元素是否有father,得知将动画应用到当前元素还是当前元素到子元素。
  336. if(currentElem.classList.contains('father')){
  337. var children = currentElem.querySelectorAll(currentElem.getAttribute('data-child'));
  338. if(flag){
  339. [].slice.call(children).forEach(function (item) {
  340. if(item.style.visibility == 'hidden'){
  341. item.style.visibility = 'visible';
  342. // 判断是否为滚动数字效果的元素
  343. // 数字滚动的效果,默认都放在father的容器里,因为这个效果一般都是多个同时出现。
  344. if(item.classList.contains('count-up')){
  345. //self.countUp(item,true);
  346. }else{
  347. if(item.getAttribute('data-effect')){
  348. item.classList.add(item.getAttribute('data-effect'));
  349. }else{
  350. item.classList.add(effect);
  351. }
  352. }
  353. }
  354. })
  355. }else{
  356. [].slice.call(children).forEach(function (item) {
  357. if(item.style.visibility == 'visible'){
  358. /*if(item.classList.contains('count-up')){
  359. self.countUp(item,false); // 传入false,告诉函数清空计时器。
  360. }*/
  361. if(item.getAttribute('data-effect')){
  362. item.classList.remove(item.getAttribute('data-effect'));
  363. }else{
  364. item.classList.remove(effect);
  365. }
  366. item.style.visibility = 'hidden';
  367. }
  368. });
  369. }
  370. }else if(currentElem.classList.contains('font-fadeIn')){ // 文字淡入到效果
  371. if(flag && currentElem.style.visibility == 'hidden'){
  372. self.fontEffect(currentElem);
  373. }else if(!flag && currentElem.style.visibility == 'visible'){
  374. currentElem.style.visibility = 'hidden'
  375. }
  376. }else if(currentElem.classList.contains('classGo')){ //滚动到位置添加动画类
  377. if(flag){
  378. currentElem.style.visibility = 'visible';
  379. currentElem.classList.add(Tclass);
  380. }else{
  381. if(currentElem.style.visibility == 'visible'){
  382. currentElem.classList.remove(Tclass);
  383. currentElem.style.visibility = 'hidden';
  384. }
  385. }
  386. }else{
  387. if(flag){
  388. currentElem.style.visibility = 'visible';
  389. currentElem.classList.add(effect);
  390. currentElem.style.animationDelay = currentElem.getAttribute('data-delay') + 's';
  391. }else{
  392. if(currentElem.style.visibility == 'visible'){
  393. currentElem.classList.remove(effect);
  394. currentElem.style.visibility = 'hidden';
  395. }
  396. }
  397. }
  398. })
  399. },
  400. countUp:function (elem,isTrue) {
  401. // 效果不理想,放弃了。
  402. if(isTrue){
  403. elem.innerHtml = '';
  404. var time = elem.getAttribute('data-time');
  405. var sum = elem.getAttribute('data-text');
  406. var speed = Math.ceil(time / 100);
  407. var increment = Math.round(sum / speed);
  408. var number = 1;
  409. elem.timer = setInterval(function () {
  410. if(number < sum){
  411. number += increment;
  412. elem.innerText = number;
  413. }else{
  414. elem.innerText = sum;
  415. clearInterval(elem.timer);
  416. }
  417. },100);
  418. console.log(speed);
  419. }else{
  420. console.log('清空定时器');
  421. clearInterval(elem.timer);
  422. }
  423. },
  424. fontEffect:function (elem) {
  425. var array = elem.getAttribute('data-text').split('');
  426. var delay = elem.getAttribute('data-delay');
  427. var effect = elem.getAttribute('data-effect') || 'fadeIn';
  428. elem.innerHTML = '';
  429. var Fragment = document.createDocumentFragment();
  430. array.forEach(function (item,i) {
  431. var span = document.createElement("font");
  432. span.className='animated';
  433. span.classList.add(effect);
  434. if(delay){
  435. span.style.animationDelay = delay * i + 's';
  436. }else{
  437. span.style.animationDelay = 0.1 * i + 's';
  438. }
  439. span.innerText = item;
  440. Fragment.appendChild(span);
  441. });
  442. elem.style.visibility = 'visible';
  443. elem.appendChild(Fragment);
  444. },
  445. init:function () {
  446. var self = this;
  447. if(self.elem.length < 1){
  448. console.log('滚动动画对象集合为零');
  449. return;
  450. }
  451. self.scrollEvent = self.scrollEvent.bind(this);
  452. setTimeout(function () {
  453. self.getPosition(); // 获取每个元素的位置,存放在一个数组。
  454. self.scrollEvent();
  455. var _scrollEvent = throttlePro(self.scrollEvent,100,300);
  456. document.addEventListener('scroll',_scrollEvent,false);
  457. // 改变窗口大小,重新初始化一些数据
  458. window.onresize = function () {
  459. //console.log('resize the window');
  460. throttle(function () {
  461. self.windowHeight = ('innerHeight' in window) ? window.innerHeight : document.documentElement.clientHeight;
  462. self.getPosition();
  463. self.scrollEvent();
  464. })
  465. };
  466. },0);
  467. }
  468. };
  469. setTimeout(function () {
  470. $.scrollAnimate = ScrollAnimate;
  471. },10);
  472. })(alan);
  473. // 函数截流
  474. function throttle(method,context){
  475. clearTimeout(method.tId);
  476. method.tId = setTimeout(function(){
  477. method.call(context);
  478. },500);
  479. }
  480. function throttlePro(fn, delay, mustRunDelay){
  481. var timer = null;
  482. var t_start;
  483. return function(){
  484. var context = this, args = arguments, t_curr = +new Date();
  485. clearTimeout(timer);
  486. if(!t_start){
  487. t_start = t_curr;
  488. }
  489. if(t_curr - t_start >= mustRunDelay){
  490. fn.apply(context, args);
  491. t_start = t_curr;
  492. }
  493. else {
  494. timer = setTimeout(function(){
  495. fn.apply(context, args);
  496. }, delay);
  497. }
  498. };
  499. }
  500. //菜单添加激活条
  501. (function($) {
  502. $.fn.lavaLamp = function(o) {
  503. o = $.extend({
  504. fx: "linear",
  505. speed: 500,
  506. click: function() {}
  507. }, o || {});
  508. return this.each(function() {
  509. var b = $(this),
  510. noop = function() {},
  511. $back = $('<div class="back"><div style="float:left;"></div></div>').appendTo(b),
  512. $li = $("li", this),
  513. curr = $("li.current-cat", this)[0] || $($li[0]).addClass("current-cat")[0];
  514. $li.not(".back").hover(function() {
  515. move(this)
  516. }, noop);
  517. $(this).hover(noop, function() {
  518. move(curr)
  519. });
  520. $li.click(function(e) {
  521. setCurr(this);
  522. return o.click.apply(this, [e, this])
  523. });
  524. setCurr(curr);
  525. function setCurr(a) {
  526. $back.css({
  527. "left": a.offsetLeft + "px",
  528. "width": a.offsetWidth + "px"
  529. });
  530. curr = a
  531. }
  532. function move(a) {
  533. $back.each(function() {
  534. $(this).dequeue()
  535. }).animate({
  536. width: a.offsetWidth,
  537. left: a.offsetLeft
  538. }, o.speed, o.fx)
  539. }
  540. })
  541. }
  542. })(jQuery);
  543. ;(function ($) {
  544. var Marqueen = function (opt) {
  545. this.opt = opt || {};
  546. this.elem = document.querySelector(this.opt.className);
  547. this.speed = this.opt.speed || 20;
  548. this.isStep = this.opt.isStep;
  549. this.ul = this.elem.querySelector('ul');
  550. this.liHeight = this.elem.querySelector('li').offsetHeight;
  551. this.ulHeight = this.ul.offsetHeight * -1;
  552. this.y = 0;
  553. this.interval = null;
  554. this.timeout = null;
  555. this.reg=/\-?[0-9]+/g;
  556. //this.reg2=/\-?[0-9]+\.?[0-9]*/g; //可能包含小数点的
  557. this.init();
  558. };
  559. Marqueen.prototype = {
  560. move:function () {
  561. var self = this;
  562. self.y --;
  563. self.ul.style.webkitTransform='translateY('+self.y+'px)';
  564. var nowY = self.ul.style.webkitTransform.match(self.reg)[0];
  565. if(self.isStep && ((-nowY) % (-self.liHeight) ===0)){
  566. clearInterval(self.interval);
  567. self.interval = null;
  568. self.timeout = setTimeout(function(){
  569. self.interval = setInterval(self.move,self.speed);
  570. },2000);
  571. }
  572. if(nowY == self.ulHeight){
  573. self.y = 0;
  574. self.ul.style.transform='translateY(0px)';
  575. }
  576. },
  577. init:function () {
  578. var self = this;
  579. self.move = self.move.bind(self);
  580. self.ul.innerHTML += self.ul.innerHTML;
  581. self.interval = setInterval(self.move,self.speed);
  582. self.elem.onmouseover = function(){
  583. clearInterval(self.interval);
  584. self.interval = null;
  585. clearTimeout(self.timeout);
  586. };
  587. self.elem.onmouseout = function(){
  588. if(self.interval == null){
  589. self.interval = setInterval(self.move,self.speed)
  590. }
  591. };
  592. }
  593. };
  594. $.Marqueen = Marqueen;
  595. })(alan);
  596. // 自定义滚动条
  597. (function($){
  598. function ScrollBar(opt){
  599. this.opt = opt || {};
  600. this.scrollBox = document.getElementsByClassName(this.opt.elem)[0];
  601. this.scrollBar = this.scrollBox.getElementsByClassName('scroll-bar')[0];
  602. this.scrollBtn = this.scrollBox.getElementsByClassName('scroll-btn')[0];
  603. this.scrollCon = this.scrollBox.getElementsByClassName('scroll-con')[0];
  604. this.scrollBtnHeight = this.scrollBtn.offsetHeight; // 按钮的高度
  605. this.scrollBoxHeight = this.scrollBox.offsetHeight; // 容器的高度
  606. this.scrollBarHeight = this.scrollBoxHeight; // 滚动条的高度
  607. this.scrollConHeight = this.scrollCon.scrollHeight - this.scrollBoxHeight; // 文章可以滚动内容高度
  608. this.MAXDIS = this.scrollBarHeight - this.scrollBtnHeight; // 可拖动最大距离
  609. this.scrollBarPosition = this.scrollBar.getBoundingClientRect().top; // 滚动条基于文档的Y距离
  610. this.mouseClickPosition; // 鼠标点击在按钮的距离
  611. this.init();
  612. }
  613. ScrollBar.prototype = {
  614. scrollTop:function(dis){
  615. var self = this;
  616. var _scrollTop = dis * self.scrollConHeight / self.MAXDIS;
  617. self.scrollCon.scrollTop = _scrollTop;
  618. },
  619. atuoMoveBtn:function(scrollTop){
  620. var self = this;
  621. var _moveY = scrollTop * self.MAXDIS / self.scrollConHeight;
  622. self.scrollBtn.style.top = _moveY + 'px';
  623. },
  624. mouseMoveEvent:function(event){
  625. var self = this;
  626. var dis = event.clientY - self.scrollBarPosition - self.mouseClickPosition;
  627. // 限制范围
  628. dis > self.MAXDIS && (dis = self.MAXDIS);
  629. dis < 0 && (dis = 0);
  630. self.scrollBtn.style.top = dis + 'px';
  631. self.scrollTop(dis);
  632. },
  633. mouseUpEvent:function(event){
  634. var self = this;
  635. self.scrollCon.classList.remove('scrolling');
  636. document.removeEventListener('mousemove',self._MoveEvent,false);
  637. document.removeEventListener('mouseup',self._UpEvent,false);
  638. },
  639. wheelEvent:function(event){
  640. var self = this;
  641. var e = event || window.event;
  642. var deltaY = e.deltaY * -30 || // wheel 事件
  643. e.wheelDeltaY/4 || // mousewheel 事件 chrome
  644. (e.wheelDeltaY === undefined && // 如果没有2D属性
  645. e.wheelDelta/4) || // 那么就用1D的滚轮属性
  646. e.detail * -10 || // firefox的DOMMouseScroll事件
  647. 0 ; // 属性未定义
  648. if ($.isMacWebkit) {
  649. deltaY /= 30;
  650. }
  651. if ($.isFirefox && e.type !== "DOMMouseScroll") {
  652. self.scrollCon.removeEventListener('DOMMouseScroll',self._wheelEvent,false);
  653. }
  654. if(!e.ctrlKey){
  655. if(deltaY > 0){
  656. //console.log(deltaY);
  657. self.scrollCon.scrollTop -= 20;
  658. }else{
  659. self.scrollCon.scrollTop += 20;
  660. }
  661. self.atuoMoveBtn(self.scrollCon.scrollTop);
  662. }
  663. e.preventDefault();
  664. e.stopPropagation();
  665. },
  666. init:function(){
  667. this._MoveEvent = this.mouseMoveEvent.bind(this);
  668. this._UpEvent = this.mouseUpEvent.bind(this);
  669. this._wheelEvent = this.wheelEvent.bind(this);
  670. var self = this;
  671. // 拖动滚轮的事件
  672. self.scrollBtn.onmousedown = function(event){
  673. var e = event || window.event;
  674. // 每次点击都获取一次按钮位置,以得到鼠标与按钮顶部的距离
  675. var scrollBtnPosition = this.offsetTop;
  676. self.mouseClickPosition = e.clientY - self.scrollBarPosition - scrollBtnPosition;
  677. self.scrollCon.classList.add('scrolling'); // 防止拖动的时候选中文字
  678. document.addEventListener('mousemove',self._MoveEvent,false);
  679. document.addEventListener('mouseup',self._UpEvent,false);
  680. };
  681. // 滚动内容的事件
  682. self.scrollCon.addEventListener('mousewheel',self._wheelEvent,false);
  683. // firefox
  684. if ($.isFirefox) {
  685. self.scrollCon.addEventListener('DOMMouseScroll',self._wheelEvent,false);
  686. }
  687. }
  688. };
  689. $.ScrollBar = ScrollBar;
  690. })(alan);