简体   繁体   English

将此 div 幻灯片的左右箭头应用 CSS 样式?

[英]Apply CSS styling to left and right arrows for this div slide?

I've got this little project I'm hoping to add to my website.我有一个希望添加到我的网站的小项目。 It's a simple div slider with arrows.这是一个带箭头的简单 div 滑块。 So far, I've coded up to this extent, and I need help to include two key features to this slider:到目前为止,我已经编写了这么多代码,我需要帮助才能在此滑块中包含两个关键功能:

------------------------------ Two features I'm trying to achieve ------------------------------ ------------------我试图实现的两个功能------------ ------------------

Feature One: I want to apply some sort of CSS styling to this slider, telling the user that they have reached the end of the slider .功能一:我想对这个滑块应用某种 CSS 样式,告诉用户他们已经到达滑块的末尾 That is, if a user clicks left to the last item, disable or change style of the left arrow.也就是说,如果用户单击左到最后一项,则禁用或更改左箭头的样式。 Likewise, if a user clicks right to the last item, disable or change style of the right arrow.同样,如果用户单击最后一项,则禁用或更改右箭头的样式。

Feature Two: You will notice that currently, there are eight div slides in the slider.特点二:你会注意到,当前,slider 中有 8 个 div 幻灯片。 Now, try commenting four of the slides.现在,尝试评论其中的四张幻灯片。 Clicking any arrow naturally wouldn't work as 4 slides is the maximum limit.单击任何箭头自然不会起作用,因为 4 张幻灯片是最大限制。 I want both arrows to be disabled when the div slides are less than or equal to 4.当 div 幻灯片小于或等于 4 时,我希望禁用两个箭头。

 let buttonLeft = document.getElementById('slide_left') let buttonRight = document.getElementById('slide_right') let container = document.getElementById('slider') buttonLeft.addEventListener('click', function() { container.scrollLeft -= 90 }) buttonRight.addEventListener('click', function() { container.scrollLeft += 90 })
 body { background-color: #555; height: 100vh; display: grid; align-items: center; justify-items: center; font-family: 'Helvetica'; } div#slide_wrapper { width: 440px; display: flex; justify-content: space-between; height: fit-content; } div#slider { width: 350px; display: flex; height: fit-content; flex-wrap: nowrap; overflow: hidden; } div.thumbnail { min-width: 80px; min-height: 80px; cursor: pointer; display: grid; place-items: center; font-size: 30px; } div.thumbnail:not(:last-child) { margin-right: 10px; } div.thumbnail:nth-child(1) { background-color: darkturquoise; } div.thumbnail:nth-child(2) { background-color: goldenrod; } div.thumbnail:nth-child(3) { background-color: rebeccapurple; } div.thumbnail:nth-child(4) { background-color: powderblue; } div.thumbnail:nth-child(5) { background-color: firebrick; } div.thumbnail:nth-child(6) { background-color: sienna; } div.thumbnail:nth-child(7) { background-color: bisque; } div.thumbnail:nth-child(8) { background-color: navy; } div#slide_wrapper>button { height: fit-content; align-self: center; font-size: 24px; font-weight: 800; border: none; outline: none; } div#slide_wrapper>button:hover { cursor: pointer; }
 <div id="slide_wrapper"> <button id="slide_left" class="slide_arrow">&#10094;</button> <div id="slider"> <div class="thumbnail active">1</div> <div class="thumbnail">2</div> <div class="thumbnail">3</div> <div class="thumbnail">4</div> <div class="thumbnail">5</div> <div class="thumbnail">6</div> <div class="thumbnail">7</div> <div class="thumbnail">8</div> </div> <button id="slide_right" class="slide_arrow">&#10095;</button> </div>

Please help me solve this.请帮我解决这个问题。

I just added a fe JS lines to do the tasks for you.我刚刚添加了 fe JS 行来为您完成任务。 If the end of oen slide is reached a class to the button is added to change the visibility to hidden .如果到达 oen slide 的末尾,则向按钮添加一个类以将visibility更改为hidden You can change the CSS settings as you deem fit or apply other changes directly through JS however you like.您可以根据需要更改 CSS 设置,也可以根据需要直接通过 JS 应用其他更改。 This example is just to show you how you could do it:此示例只是向您展示如何做到这一点:

At start I added a few variables:在开始时,我添加了一些变量:
slidesAmount => The number of your total slides. slidesAmount => 幻灯片总数。 EDIT: Is now calculated by the script so you do not have tos et it manually编辑:现在由脚本计算,因此您不必手动设置它
slidesPerScreen => How many slides are dispalyed at the same time on screen slidesPerScreen => 屏幕上同时显示多少张幻灯片
currentSlideCounter => The slide numebr that is display at the very left currentSlideCounter => 显示在最左边的幻灯片编号
maxSlideCounter => is a calculated variable to mark the end of the possible slides to the right. maxSlideCounter => 是一个计算变量,用于将可能的幻灯片结束标记到右侧。

Then I added to your functiosn an if -statement to raise or lower the counter currentSledeCounter to keep it updated.然后我在你的函数中添加了一个if语句来提高或降低计数器currentSledeCounter以保持更新。 The if -statement ensures that you dont lwoer or raise that counter when you reached that specific end. if语句可确保您在达到特定目标时不会停止或提高该计数器。

Last but not least I added a function with a switch -statement that applies a class to the button when an end has reached or removes the classes when not end is reached yet.最后但并非最不重要的一点是,我添加了一个带有switch语句的函数,该语句在到达终点时将类应用于按钮,或者在尚未到达终点时移除类。

EDIT: I added an eventLister onload to run the script at the start so that by default the left arrow is hidden.编辑:我添加了一个eventLister onload 以在开始时运行脚本,因此默认情况下左箭头是隐藏的。

EDIT 2: There was a bug that occured because of the switch-statement logic if there is just one more slideAmount then the slidesPerScreen .编辑 2:如果只有一个slideAmount然后是slidesPerScreen ,则由于switch-statement逻辑而发生了一个错误。 So I remoevd the switch statement and simpliefied the logic.所以我删除了 switch 语句并简化了逻辑。 It now removes all class by start and adds the class if a certain end has reached:它现在按开始删除所有类,并在达到某个结束时添加该类:

 /* eventListener to run the script at the start and hdie left button automatically */ window.addEventListener("load", change); var slidesAmount = document.querySelectorAll('#slider .thumbnail').length, slidesPerScreen = 4; var currentSlideCounter = 1; var maxSlideCounter = slidesAmount - slidesPerScreen + 1; let buttonLeft = document.getElementById('slide_left') let buttonRight = document.getElementById('slide_right') let container = document.getElementById('slider') buttonLeft.addEventListener('click', function() { container.scrollLeft -= 90; if (currentSlideCounter > 1) { currentSlideCounter -= 1; } change(); }) buttonRight.addEventListener('click', function() { container.scrollLeft += 90; if (currentSlideCounter < maxSlideCounter) { currentSlideCounter += 1; } change(); }) function change() { document.querySelectorAll('.slide_arrow').forEach(el => el.classList.remove('changes')); if (currentSlideCounter == 1) { document.querySelector('#slide_left').classList.add('changes'); } if (currentSlideCounter == maxSlideCounter) { document.querySelector('#slide_right').classList.add('changes'); } }
 body { background-color: #555; height: 100vh; display: grid; align-items: center; justify-content: center; /* justify-items is incorrect, it's justify-content */ font-family: 'Helvetica'; } div#slide_wrapper { width: 440px; display: flex; justify-content: space-between; height: fit-content; } div#slider { width: 350px; display: flex; height: fit-content; flex-wrap: nowrap; overflow: hidden; } div.thumbnail { min-width: 80px; min-height: 80px; cursor: pointer; display: grid; place-items: center; font-size: 30px; } div.thumbnail:not(:last-child) { margin-right: 10px; } div.thumbnail:nth-child(1) { background-color: darkturquoise; } div.thumbnail:nth-child(2) { background-color: goldenrod; } div.thumbnail:nth-child(3) { background-color: rebeccapurple; } div.thumbnail:nth-child(4) { background-color: powderblue; } div.thumbnail:nth-child(5) { background-color: firebrick; } div.thumbnail:nth-child(6) { background-color: sienna; } div.thumbnail:nth-child(7) { background-color: bisque; } div.thumbnail:nth-child(8) { background-color: navy; } div#slide_wrapper>button { height: fit-content; align-self: center; font-size: 24px; font-weight: 800; border: none; outline: none; } div#slide_wrapper>button:hover { cursor: pointer; } /* this CSS line applies changes with the JS script */ button.changes { visibility: hidden; }
 <div id="slide_wrapper"> <button id="slide_left" class="slide_arrow">&#10094;</button> <div id="slider"> <div class="thumbnail active">1</div> <div class="thumbnail">2</div> <div class="thumbnail">3</div> <div class="thumbnail">4</div> <div class="thumbnail">5</div> <div class="thumbnail">6</div> <div class="thumbnail">7</div> <div class="thumbnail">8</div> </div> <button id="slide_right" class="slide_arrow">&#10095;</button> </div>

While the above answer works, you might also be able to do this with the scrollWidth property.虽然上述答案有效,但您也可以使用scrollWidth属性来做到这一点。

Using the steps to determine whether the element is fully scrolled and using the widths instead of heights, you can apply CSS classes to the buttons based on the thresholds you set.使用这些步骤来确定元素是否完全滚动并使用宽度而不是高度,您可以根据您设置的阈值将 CSS 类应用于按钮。

I might update the answer to include a code example when I get home, but I think the docs will help you out a lot.当我回家时,我可能会更新答案以包含一个代码示例,但我认为这些文档会对你有很大帮助。

Update:更新:

As stated, I made a version which disables the buttons according to the scrolling- and clientWidth.如前所述,我制作了一个根据滚动和客户端宽度禁用按钮的版本。 The advantage of this is that there is no need to keep track of the amount of thumbnails, and will always work as long as you add same-sized thumbnails to your slide.这样做的好处是无需跟踪缩略图的数量,只要您在幻灯片中添加相同大小的缩略图,它就会始终有效。

Besides changing the JS file, I added .deactivated to your css file, and assigned this class to the left button in the html file.除了更改 JS 文件外,我还在您的 css 文件中添加了.deactivated ,并将此类分配给 html 文件中的左侧按钮。

Hope this helps!希望这可以帮助!

Update 2: Fixed the issue mentioned in the comment by Matrion.更新 2:修复了 Matrion 评论中提到的问题。

 let buttonLeft = document.getElementById("slide_left"); let buttonRight = document.getElementById("slide_right"); let container = document.getElementById("slider"); // Initial calculation of how much their is to scroll, if none, disable // both buttons since every thumbnail fits in the view. let scrollingLeft = container.scrollWidth - container.clientWidth - container.scrollLeft; if (scrollingLeft === 0) { buttonLeft.classList.add("deactivated"); buttonRight.classList.add("deactivated"); } else { buttonLeft.addEventListener("click", function() { container.scrollLeft -= 90; if (container.scrollLeft === 0) { buttonLeft.classList.add("deactivated"); buttonRight.classList.remove("deactivated"); } else { buttonLeft.classList.remove("deactivated"); buttonRight.classList.remove("deactivated"); } }); buttonRight.addEventListener("click", function() { container.scrollLeft += 90; let scrollingLeft = container.scrollWidth - container.clientWidth - container.scrollLeft; if (scrollingLeft === 0) { buttonRight.classList.add("deactivated"); buttonLeft.classList.remove("deactivated"); } else { buttonRight.classList.remove("deactivated"); buttonLeft.classList.remove("deactivated"); } }); }
 body { background-color: #555; height: 100vh; display: grid; align-items: center; justify-items: center; font-family: "Helvetica"; } div#slide_wrapper { width: 440px; display: flex; justify-content: space-between; height: fit-content; } div#slider { width: 350px; display: flex; height: fit-content; flex-wrap: nowrap; overflow: hidden; } div.thumbnail { min-width: 80px; min-height: 80px; cursor: pointer; display: grid; place-items: center; font-size: 30px; } div.thumbnail:not(:last-child) { margin-right: 10px; } div.thumbnail:nth-child(1) { background-color: darkturquoise; } div.thumbnail:nth-child(2) { background-color: goldenrod; } div.thumbnail:nth-child(3) { background-color: rebeccapurple; } div.thumbnail:nth-child(4) { background-color: powderblue; } div.thumbnail:nth-child(5) { background-color: firebrick; } div.thumbnail:nth-child(6) { background-color: sienna; } div.thumbnail:nth-child(7) { background-color: bisque; } div.thumbnail:nth-child(8) { background-color: navy; } div#slide_wrapper>button { height: fit-content; align-self: center; font-size: 24px; font-weight: 800; border: none; outline: none; } div#slide_wrapper>button:hover { cursor: pointer; } .deactivated { background-color: grey; }
 <div id="slide_wrapper"> <button id="slide_left" class="slide_arrow deactivated">&#10094;</button> <div id="slider"> <div class="thumbnail active">1</div> <div class="thumbnail">2</div> <div class="thumbnail">3</div> <div class="thumbnail">4</div> <div class="thumbnail">5</div> <div class="thumbnail">6</div> <div class="thumbnail">7</div> <div class="thumbnail">8</div> </div> <button id="slide_right" class="slide_arrow">&#10095;</button> </div>

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

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