怎么在JavaScript中利用面向对象实现一个无缝滚动轮播?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
JavaScript面向对象部分如下:
function Seamless(obj) { this.obj = document.getElementById(obj); this.oXSlide = GLOBAL.Dom.getElementsByClassName('J_XSlide', this.obj)[0]; this.li = this.oXSlide.getElementsByTagName('li'); this.liTabs = GLOBAL.Dom.getElementsByClassName('tabs', this.obj)[0].getElementsByTagName('li'); this.iNow = 0; this.timer = null; this.playTime = 2000; this.init(); } Seamless.prototype = { // 初始化 init: function() { var _this = this; this.timer = setInterval(function(){ _this.autoPlay(); }, this.playTime); this.overOut(); this.tab(); }, tab: function() { var _this = this; var btn = GLOBAL.Dom.getElementsByClassName('btn', this.obj); this.oXSlide.style.width = this.li.length * this.li[0].offsetWidth + 'px'; for(var i = 0, len = this.liTabs.length; i < len; i++) { this.liTabs[i].index = i; GLOBAL.Event.addHandler(this.liTabs[i], 'mouseover', function() { _this.showItem(this.index); }); } GLOBAL.Event.addHandler(btn[0], 'click', function() { _this.moveLeft(); }); GLOBAL.Event.addHandler(btn[1], 'click', function() { _this.moveRight(); }); }, // 移入移出时,轮播暂停与开始 overOut: function() { var _this = this; GLOBAL.Event.addHandler(this.obj, 'mouseover', function() { clearInterval(_this.timer); }); GLOBAL.Event.addHandler(this.obj, 'mouseout', function() { _this.timer = setInterval(function() { _this.autoPlay(); }, _this.playTime); }); }, autoPlay: function() { this.moveRight(); }, // 选项卡 showItem:function (n) { for(var i = 0, len = this.liTabs.length; i < len; i++) { this.liTabs[i].className = 'tab'; } if(n == this.liTabs.length) { this.liTabs[0].className = 'tab cur'; } else { this.liTabs[n].className = 'tab cur'; } startMove(this.oXSlide, {'left': -n * this.li[0].offsetWidth}); }, moveLeft:function () { this.iNow--; if(this.iNow == -1) { this.oXSlide.style.left = -this.liTabs.length * this.li[0].offsetWidth + 'px'; this.iNow = this.liTabs.length - 1; } this.showItem(this.iNow); }, moveRight:function () { this.iNow++; if(this.iNow == this.li.length) { this.oXSlide.style.left = 0; this.iNow = 1; } this.showItem(this.iNow); } }; new Seamless('box');
关于怎么在JavaScript中利用面向对象实现一个无缝滚动轮播问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。