Javascript 幻灯片放映为更改图像和按钮添加和删除类
我想做幻灯片放映。 这是我的代码: java脚本=>Javascript 幻灯片放映为更改图像和按钮添加和删除类,javascript,jquery,Javascript,Jquery,我想做幻灯片放映。 这是我的代码: java脚本=> setInterval(函数(){ var activeLi=document.querySelector('li.current'); activeLi.classList.remove('current'); if(activeLi.nextElementSibling){ activeLi.nextElementSibling.classList.add('current'); }否则{ activeLi.parentElement.f
setInterval(函数(){
var activeLi=document.querySelector('li.current');
activeLi.classList.remove('current');
if(activeLi.nextElementSibling){
activeLi.nextElementSibling.classList.add('current');
}否则{
activeLi.parentElement.firstElementChild.classList.add('当前')
}
var activeIMG=document.querySelector('.active_slider');
activeIMG.classList.remove('active_slider');
if(activeIMG.nextElementSibling){
activeIMG.nextElementSibling.classList.add('active_slider');
}否则{
activeIMG.parentElement.firstElementChild.classList.add('active\u slider')
}
}, 5000);代码>
。活动\u滑块{
显示:内联;
}
.当前{
颜色:红色;
}
-
幻灯片1
-
幻灯片2
-
幻灯片3
您的html无效,在类
之后缺少=
,应该是class=“slide1 active”
。若要在一段时间后调用函数,可以使用adn修改可以修改的类
财产:
var x=1000;
setInterval(函数(){
var-activeLi=document.querySelector('li.active');
activeLi.classList.remove('active');
if(activeLi.nextElementSibling){
activeLi.nextElementSibling.classList.add('active');
}否则{
activeLi.parentElement.firstElementChild.classList.add('active'))
}
},x)代码>
.active{
颜色:红色
}
尝试类似的操作,它将每3秒运行一次,并将类设置为活动的下一个元素
setInterval(函数(){
变量滑块=$(“.slider”);
var active=slider.find(“.active”);
var sliderCount=slider.find(“li”).length;
var index=active.index();
active.removeClass(“active”);
如果(索引<滑块计数-1){
active.next().addClass(“active”)
}否则{
slider.find(“li:first”).addClass(“active”)
}
}, 3000);代码>
.active{
颜色:黄色
}
欢迎使用StackOverflow可能重复!到目前为止你试过什么吗?StackOverflow不是一个免费的代码编写服务,它希望您能够这样做。请更新您的问题,以显示您已经尝试过的内容,并显示您在某个问题中面临的具体问题。有关更多信息,请参阅,并以google#1:javascript如何添加类为例。google#2:javascript超时所有幻灯片的自动程度如何?slide1、slide2、slide3和再次如果我的代码没有标记,如何在document.querySelector中找到它?例如:document.querySelector('img.active');document.querySelector('img.active')应该和document.querySelector('.active')一样工作。我更改了文本和代码,然后。。。你能帮助我吗?