Javascript JS滑块不工作
我有一个用于slider的js类,但是在将js连接到html时出错了,我有一个用于slider、slider容器、箭头和点的类。 请帮帮我Javascript JS滑块不工作,javascript,slider,native,Javascript,Slider,Native,我有一个用于slider的js类,但是在将js连接到html时出错了,我有一个用于slider、slider容器、箭头和点的类。 请帮帮我 <div class="b-slider"> <a href="#" class="arrow-right js-right"></a> <a href="#" class="arrow-left js-left"></a> <ul class="slider js-slides">
<div class="b-slider">
<a href="#" class="arrow-right js-right"></a>
<a href="#" class="arrow-left js-left"></a>
<ul class="slider js-slides">
<li class="slide-one js-slide"></li>
<li class="slide-two js-slide"></li>
<li class="slide-three js-slide"></li>
</ul>
<ul class="dots">
<li class="dot js-bull"><a class="show-slider"></a></li>
<li class="dot js-bull active"><a class="show-slider"></a></li>
<li class="dot js-bull"><a class="show-slider"></a></li>
</ul>
和通过JS的滑块类
class Slider {
constructor (root, options = {}) {
var defaultOptions = {};
this.root = root;
this.options = _.assign(defaultOptions, options);
this.itemsCount = 0;
this.itemWidth = 0;
this.currentIndex = 0;
this._cacheNodes();
this._initialize();
this._bindEvents();
}
_cacheNodes () {
this.nodes = {
slidesContainer: this.root.find('.js-slides'),
slides: this.root.find('.js-slide'),
left: this.root.find('.js-left'),
right: this.root.find('.js-right'),
bulls: this.root.find('.js-bull')
};
}
_initialize () {
this.itemsCount = this.nodes.slides.length;
this.itemWidth = this.nodes.slides.eq(0).outerWidth(true);
this.nodes.slidesContainer.width(this.itemWidth * (this.itemsCount));
this._goTo(this.currentIndex);
}
_bindEvents () {
$$.window.on('resize', () => {
this._initialize();
});
this.nodes.left.on('click', () => {
this._goTo(this.currentIndex - this.OnScreenCount);
});
this.nodes.right.on('click', () => {
this._goTo(this.currentIndex + this.OnScreenCount);
});
if (this.nodes.bulls.length) {
this.nodes.bulls.on('click', (event) => {
this._goTo($(event.currentTarget).index());
});
}
}
_goTo (index) {
if (index <= 0) {
this.nodes.left.addClass('disabled');
} else {
this.nodes.left.removeClass('disabled');
}
if (index > this.itemsCount - 1 - this.screenCount) {
this.nodes.right.addClass('disabled');
} else {
this.nodes.right.removeClass('disabled');
}
if ((index > this.itemsCount - this.screenCount) || (index < 0)) {
return;
}
this.nodes.slidesContainer.css({
transform: `translateX(${ -index * this.itemWidth }px)`
});
if (this.nodes.bulls.length) {
this.nodes.bulls.eq(this.currentIndex).removeClass('active');
}
this.currentIndex = index;
if (this.nodes.bulls.length) {
this.nodes.bulls.eq(this.currentIndex).addClass('active');
}
}
类滑块{
构造函数(根,选项={}){
var defaultOptions={};
this.root=根;
this.options=\赋值(defaultOptions,options);
this.itemsunt=0;
这个.itemWidth=0;
此.currentIndex=0;
这是._cacheNodes();
这个;
这个;
}
_缓存节点(){
此节点数={
SlideContainer:this.root.find('.js slides'),
幻灯片:this.root.find(“.js幻灯片”),
左:this.root.find('.js left'),
右:this.root.find('.js right'),
bulls:this.root.find('.jsbull')
};
}
_初始化(){
this.itemsont=this.nodes.slides.length;
this.itemWidth=this.nodes.slides.eq(0).outerWidth(true);
this.nodes.slideContainer.width(this.itemWidth*(this.itemsCount));
此._goTo(此.currentIndex);
}
_bindEvents(){
$$.window.on('resize',()=>{
这个;
});
this.nodes.left.on('click',()=>{
this.\u goTo(this.currentIndex-this.OnScreenCount);
});
this.nodes.right.on('click',()=>{
this._goTo(this.currentIndex+this.OnScreenCount);
});
if(this.nodes.bulls.length){
this.nodes.bulls.on('单击',(事件)=>{
此.u转到($(event.currentTarget.index());
});
}
}
_后藤(索引){
if(索引this.itemsunt-1-this.screenCount){
this.nodes.right.addClass('disabled');
}否则{
this.nodes.right.removeClass('disabled');
}
if((index>this.itemsunt-this.screenCount)| |(index<0)){
返回;
}
this.nodes.slidecontainer.css({
transform:`translateX(${-index*this.itemWidth}px)`
});
if(this.nodes.bulls.length){
this.nodes.bulls.eq(this.currentIndex.removeClass('active');
}
this.currentIndex=索引;
if(this.nodes.bulls.length){
this.nodes.bulls.eq(this.currentIndex.addClass('active');
}
}