简体   繁体   English

幻灯片 - 根据 position 禁用左键或右键

[英]slideshow - disable left or right button based on position

I have coded a small slideshow script, which scrolls left and right.我编写了一个小的幻灯片脚本,可以左右滚动。

I am hoping for some advice or guidance;我希望得到一些建议或指导; as I am stuck for inspiration or a possible solution.因为我一直在寻找灵感或可能的解决方案。

How would you identify if you are at the beginning or end, to respectively disable the left or right button?你会如何识别你是在开始还是结束,分别禁用左键或右键?

 window.addEventListener('DOMContentLoaded', () => { let one = new slideShow('merchandising'); }) function slideShow(element) { this.scrollContainer = document.getElementById(element); this.scrollLeft = this.scrollContainer.getElementsByClassName('left')[0]; this.scrollRight = this.scrollContainer.getElementsByClassName('right')[0]; this.move = (amount) => { this.scrollContainer.scrollLeft += amount; } this.scrollLeft.addEventListener('click', () => { this.move(-225); }) this.scrollRight.addEventListener('click', () => { this.move(225) }) }
 #merchandising { overflow-x: scroll; white-space: nowrap; }.slot { height: 200px; width: 200px; background: red; padding: 5px; margin: 5px; display: inline-block; }.left { display: block; position: absolute; left: 0; }.right { display: block; position: absolute; right: 0; }
 <div id='merchandising'> <button class='left'>left</button> <button class='right'>right</button> <div class='slot'>one</div> <div class='slot'>two</div> <div class='slot'>three</div> <div class='slot'>four</div> <div class='slot'>five</div> <div class='slot'>six</div> <div class='slot'>seven</div> <div class='slot'>eight</div> <div class='slot'>nine</div> <div class='slot'>ten</div> </div>

Its not the complete logic for a slider but i think you get the idea:)它不是 slider 的完整逻辑,但我想你明白了:)

 window.addEventListener('DOMContentLoaded', () => { let one = new slideShow('merchandising'); }) function slideShow(element) { this.scrollContainer = document.getElementById(element); this.scrollLeft = this.scrollContainer.getElementsByClassName('left')[0]; this.scrollRight = this.scrollContainer.getElementsByClassName('right')[0]; this.move = (amount) => { this.scrollContainer.scrollLeft += amount; } this.scrollLeft.addEventListener('click', () => { this.move(-225); if (this.scrollContainer.scrollLeft === 0) { document.getElementsByClassName('left')[0].classList.remove('previous'); } }) this.scrollRight.addEventListener('click', () => { this.move(225) if (this.scrollContainer.scrollLeft > 0) { document.getElementsByClassName('left')[0].classList.add('previous'); } }) }
 #merchandising { overflow-x: scroll; white-space: nowrap; }.slot { height: 200px; width: 200px; background: red; padding: 5px; margin: 5px; display: inline-block; }.left { display: none; position: absolute; left: 0; }.left.previous { display: block; }.right { display: block; position: absolute; right: 0; }
 <div id='merchandising'> <button class='left'>left</button> <button class='right'>right</button> <div class='slot'>one</div> <div class='slot'>two</div> <div class='slot'>three</div> <div class='slot'>four</div> <div class='slot'>five</div> <div class='slot'>six</div> <div class='slot'>seven</div> <div class='slot'>eight</div> <div class='slot'>nine</div> <div class='slot'>ten</div> </div>

You need to check on right click function like ((this.scrollContainer.scrollLeft + this.scrollContainer.offsetWidth) == this.scrollContainer.scrollWidth)您需要检查右键单击 function,例如((this.scrollContainer.scrollLeft + this.scrollContainer.offsetWidth) == this.scrollContainer.scrollWidth)

I hope this snippet will help you lot.我希望这个片段对你有很大帮助。

 window.addEventListener('DOMContentLoaded', () => { let one = new slideShow('merchandising'); }) function slideShow(element) { this.scrollContainer = document.getElementById(element); this.scrollLeft = this.scrollContainer.getElementsByClassName('left')[0]; this.scrollRight = this.scrollContainer.getElementsByClassName('right')[0]; this.move = (amount) => { this.scrollContainer.scrollLeft += amount; } this.scrollLeft.addEventListener('click', () => { this.move(-225); document.getElementsByClassName('right')[0].classList.remove('hide'); if (this.scrollContainer.scrollLeft === 0) { document.getElementsByClassName('left')[0].classList.remove('previous'); } }) this.scrollRight.addEventListener('click', () => { this.move(225); document.getElementsByClassName('right')[0].classList.remove('hide'); if (this.scrollContainer.scrollLeft > 0) { document.getElementsByClassName('left')[0].classList.add('previous'); if((this.scrollContainer.scrollLeft + this.scrollContainer.offsetWidth) == this.scrollContainer.scrollWidth){ document.getElementsByClassName('right')[0].classList.add('hide'); } } }) }
 #merchandising { overflow-x: scroll; white-space: nowrap; }.slot { height: 200px; width: 200px; background: red; padding: 5px; margin: 5px; display: inline-block; }.left { display: none; position: absolute; left: 0; }.left.previous { display: block; }.right { display: block; position: absolute; right: 0; }.right.hide { display: none; }
 <div id='merchandising'> <button class='left'>left</button> <button class='right'>right</button> <div class='slot'>one</div> <div class='slot'>two</div> <div class='slot'>three</div> <div class='slot'>four</div> <div class='slot'>five</div> <div class='slot'>six</div> <div class='slot'>seven</div> <div class='slot'>eight</div> <div class='slot'>nine</div> <div class='slot'>ten</div> </div>

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

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