简体   繁体   English

更改轮播上 div 的背景颜色

[英]change background colour of div on carousel

I'm trying to get to grips with javascript, and have followed a tutorial for a simple image slider.我正在尝试掌握 javascript,并遵循了简单图像 slider 的教程。 I'm trying to add to it and have the background fade to different colours as the slides move.我正在尝试添加它并随着幻灯片的移动使背景褪色为不同的颜色。 I've managed to figure it out with the right and left arrows (not sure on best practise), but I can't seem to get it right when selecting the indicators.我已经设法用左右箭头弄清楚了(不确定最佳实践),但在选择指标时我似乎无法正确理解。 Can anyone advise on a solution?任何人都可以就解决方案提出建议吗?

Thanks in advance.提前致谢。

 const left = document.querySelector('.left'); const right = document.querySelector('.right'); const slider = document.querySelector('.carousel__slider'); const indicatorParent = document.querySelector('.carousel__controls ol'); const indicators = document.querySelectorAll('.carousel__controls li'); index = 0; var background = 1; function indicatorBg(val){ var background = val; changeBg(); } indicators.forEach((indicator, i) => { indicator.addEventListener('click', () => { document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicator.classList.add('selected'); slider.style.transform = 'translateX(' + (i) * -25 + '%)'; index = i; }); }); left.addEventListener('click', function() { index = (index > 0)? index -1: 0; document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicatorParent.children[index].classList.add('selected'); slider.style.transform = 'translateX(' + (index) * -25 + '%)'; if (background <= 1) { return false; } else { background--; } changeBg(); }); right.addEventListener('click', function() { index = (index < 4 - 1)? index+1: 3; document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicatorParent.children[index].classList.add('selected'); slider.style.transform = 'translateX(' + (index) * -25 + '%)'; if (background >= 4) { return false; } else { background++; } changeBg(); }); function changeBg (){ if (background == 1) { document.getElementById("carousel__track").className = 'slide-1'; } else if (background == 2) { document.getElementById("carousel__track").className = 'slide-2'; } else if (background == 3) { document.getElementById("carousel__track").className = 'slide-3'; } else if (background == 4) { document.getElementById("carousel__track").className = 'slide-4'; } } window.onload = changeBg;
 .carousel { height: 80vh; width: 100%; margin: 0 auto; } #carousel__track { height: 100%; position: relative; overflow: hidden; }.background { background: red; }.carousel__slider { height: 100%; display: flex; width: 400%; transition: all 0.3s; }.carousel__slider div { flex-basis: 100%; display: flex; justify-content: center; align-items: center; }.carousel__controls.carousel__arrow { position: absolute; top: 50%; transform: translateY(-50%); cursor: pointer; z-index: 8888 }.carousel__controls.carousel__arrow i { font-size: 2.6rem; }.carousel__arrow.left { left: 1em; }.carousel__arrow.right { right: 1em; }.carousel__controls ol { position: absolute; bottom: 15%; left: 50%; transform: translateX(-50%); list-style: none; display: flex; margin: 0; padding: 0; }.carousel__controls ol li { width: 14px; height: 14px; border-radius: 50px; margin: .5em; padding: 0; background: white; transform: scale(.6); cursor: pointer; }.carousel__controls ol li.selected { background: black; transform: scale(1); transition: all.2s; transition-delay: .3s; }.slide-1 { background: pink; transition: all 0.4s; }.slide-2 { background: coral; transition: all 0.4s; }.slide-3 { background: green; transition: all 0.4s; }.slide-4 { background: orange; transition: all 0.4s; }
 <section class="carousel"> <div id="carousel__track"> <div class="carousel__slider"> <div>Slide 1</div> <div>Slide 2</div> <div>Slide 3</div> <div>Slide 4</div> </div> <div id="left" class="carousel__controls"><span class="carousel__arrow left"><</span> <span id="right" class="carousel__arrow right">></span> <ol> <li value="1" onclick="indicatorBg(this.value)" class="selected"></li> <li value="2" onclick="indicatorBg(this.value)"></li> <li value="3" onclick="indicatorBg(this.value)"></li> <li value="4" onclick="indicatorBg(this.value)"></li> </ol> </div> </div> </section>

You forgot to change the background inside the click event handler of the indicators.您忘记更改指标的单击事件处理程序中的背景。

indicators.forEach((indicator, i) => {
  indicator.addEventListener('click', () => {
    document.querySelector('.carousel__controls .selected').classList.remove('selected');
    indicator.classList.add('selected');
    slider.style.transform = 'translateX(' + (i) * -25 + '%)';  
    index = i;

    background = index + 1;
    changeBg();
  });
});

As far as best practice goes, I typically use class names for CSS and IDs for JavaScript.就最佳实践而言,我通常对 CSS 使用 class 名称,对 JavaScript 使用 ID。 Personally, I wouldn't recommend you worry about best practices at this stage, but instead, focus on getting the code working and understanding what's going on line-by-line.就个人而言,我不建议您在此阶段担心最佳实践,而是专注于让代码正常工作并逐行了解正在发生的事情。

There is a lot of solutions, but the simplest solution that I advice is to use odd and even numbers to style the divs in the carousel (meaning that eg. first is green second is orange third is green and so on...有很多解决方案,但我建议的最简单的解决方案是使用奇数和偶数来设置轮播中的 div 样式(这意味着例如第一个是绿色第二个是橙色第三个是绿色等等......

.carousel__slider div:nth-child(2n) /*Selects even numbered elements*/
.carousel__slider div:nth-child(2n+1) /*Selects odd numbered elements*/

Check out the snippet查看片段

 const left = document.querySelector('.left'); const right = document.querySelector('.right'); const slider = document.querySelector('.carousel__slider'); const indicatorParent = document.querySelector('.carousel__controls ol'); const indicators = document.querySelectorAll('.carousel__controls li'); index = 0; //var background = 1; //function indicatorBg(val){ // var background = val; // changeBg(); //} indicators.forEach((indicator, i) => { indicator.addEventListener('click', () => { document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicator.classList.add('selected'); slider.style.transform = 'translateX(' + (i) * -25 + '%)'; index = i; }); }); left.addEventListener('click', function() { index = (index > 0)? index -1: 0; document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicatorParent.children[index].classList.add('selected'); slider.style.transform = 'translateX(' + (index) * -25 + '%)'; // if (background <= 1) { // return false; // } else { // background--; // } // changeBg(); }); right.addEventListener('click', function() { index = (index < 4 - 1)? index+1: 3; document.querySelector('.carousel__controls.selected').classList.remove('selected'); indicatorParent.children[index].classList.add('selected'); slider.style.transform = 'translateX(' + (index) * -25 + '%)'; // if (background >= 4) { // return false; // } else { // background++; // } // changeBg(); }); //function changeBg (){ // if (background == 1) { // document.getElementById("carousel__track").className = 'slide-1'; // } else if (background == 2) { // document.getElementById("carousel__track").className = 'slide-2'; // } else if (background == 3) { // document.getElementById("carousel__track").className = 'slide-3'; // } else if (background == 4) { // document.getElementById("carousel__track").className = 'slide-4'; // } //} //window.onload = changeBg;
 .carousel { height: 80vh; width: 100%; margin: 0 auto; } #carousel__track { height: 100%; position: relative; overflow: hidden; }.background { background: red; }.carousel__slider { height: 100%; display: flex; width: 400%; transition: all 0.3s; }.carousel__slider div { flex-basis: 100%; display: flex; justify-content: center; align-items: center; }.carousel__controls.carousel__arrow { position: absolute; top: 50%; transform: translateY(-50%); cursor: pointer; z-index: 8888 }.carousel__controls.carousel__arrow i { font-size: 2.6rem; }.carousel__arrow.left { left: 1em; }.carousel__arrow.right { right: 1em; }.carousel__controls ol { position: absolute; bottom: 15%; left: 50%; transform: translateX(-50%); list-style: none; display: flex; margin: 0; padding: 0; }.carousel__controls ol li { width: 14px; height: 14px; border-radius: 50px; margin: .5em; padding: 0; background: white; transform: scale(.6); cursor: pointer; }.carousel__controls ol li.selected { background: black; transform: scale(1); transition: all.2s; transition-delay: .3s; } /*.slide-1 { background: pink; transition: all 0.4s; }.slide-2 { background: coral; transition: all 0.4s; }.slide-3 { background: green; transition: all 0.4s; }.slide-4 { background: orange; transition: all 0.4s; }*/.carousel__slider div:nth-child(2n) { background-color:orange; }.carousel__slider div:nth-child(2n+1) { background-color:green; }
 <section class="carousel"> <div id="carousel__track"> <div class="carousel__slider"> <div>Slide 1</div> <div>Slide 2</div> <div>Slide 3</div> <div>Slide 4</div> </div> <div id="left" class="carousel__controls"><span class="carousel__arrow left"><</span> <span id="right" class="carousel__arrow right">></span> <ol> <li value="1" class="selected"></li> <li value="2" ></li> <li value="3" ></li> <li value="4" ></li> </ol> </div> </div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM