Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - How to move the ID of one element to another using JS

I am trying to create a slide show of icons. All the icons are in one class. Another style class called "active" will have an ID and represents the current picture. Clicking the left or right button should change the style of the active class.

const leftArrow = document.getElementById('left-arrow');
const activeImg = document.getElementById('active') const nextImg = activeImg.nextElementSibling; // Handle left click leftArrow.addEventListener('click', function(){ activeImg.classList.remove('active');
nextImg.classList.add('active');
return nextImg
});
.icon-container i {
  font-size: 150px;
  position: relative;
  left: 12rem;
  top: 12rem;
  z-index: -10;
  display: none;
}

#active {
  z-index: 10;
  display: flex;
}

.left-arrow,
.right-arrow {
  width: 50px;
  height: 50px;
  transition: .5s;
  float: left;
  box-shadow: -2px 2px 0 rgba(238, 0, 0, 0.5);
  cursor: pointer;
}
<div class="object-container">

  <div class="icon-container">
    <i class="fa fa-car" id="active"></i>
    <i class="fa fa-bicycle"></i>
    <i class="fa fa-plane"></i>
    <i class="fa fa-ship"></i>
    <i class="fa fa-fighter-jet"></i>
    <i class="fa fa-space-shuttle"></i>
  </div>
  <div class="arrow-buttons">
    <a href="" class="right-arrow" id="right-arrow"></a>
    <a href="" class="left-arrow" id="left-arrow"></a>
  </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recommend using a class name and not an id. Here's how you can do that:

https://jsfiddle.net/sauz07d5/1


 <div class="object-container">

    <div class="icon-container">
        <i class="fa fa-car active"></i>
        <i class="fa fa-bicycle"></i>
        <i class="fa fa-plane"></i>
        <i class="fa fa-ship"></i>
        <i class="fa fa-fighter-jet"></i>
        <i class="fa fa-space-shuttle"></i>
    </div>
    <div class="arrow-buttons">
        <button class="right-arrow" id="right-arrow"></button>
        <button class="left-arrow" id="left-arrow"></button>
    </div>
    </div>
.icon-container i{
font-size: 150px;
position: relative;
left: 12rem;
top: 12rem;
z-index: -10;
display: none;
}



.left-arrow, .right-arrow{
width: 50px;
height: 50px;
transition: .5s;
float: left;
box-shadow: -2px 2px 0 rgba(238, 0, 0, 0.5);
cursor: pointer;
}

.active {
z-index: 10 !important;
display: flex !important;
}
const leftArrow = document.getElementById('left-arrow');
const rightArrow = document.getElementById('right-arrow');



// Handle left click

var element = document.querySelector(".icon-container")

function slideNext() {
let currentImg = document.querySelector(".active");
let nextImg = currentImg.nextElementSibling ? currentImg.nextElementSibling : element.children[0];

currentImg.classList.remove("active");
nextImg.classList.add("active");
}

function slidePrevious() {
let currentImg = document.querySelector(".active");
let previousImg = currentImg.previousElementSibling ? currentImg.previousElementSibling : element.children[element.children.length - 1];

currentImg.classList.remove("active");
previousImg.classList.add("active");
}


leftArrow.addEventListener('click', slideNext);
leftArrow.addEventListener('touch', slideNext);

rightArrow.addEventListener('click', slidePrevious);
rightArrow.addEventListener('touch', slidePrevious);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...