【问题标题】:How to change image using arrow keys using javascript如何使用javascript使用箭头键更改图像
【发布时间】:2021-08-29 12:05:14
【问题描述】:

所以我有这个画廊。当用户在模式弹出后按下箭头键时,我需要更改图像。左箭头键改变图像在左和右箭头键为右。我对javascript很陌生,所以如果你能帮助我,那就太好了。 全文请看代码sn-p。

function openModal() {
  document.getElementById("myModal").style.display = "block";
}

function closeModal() {
  document.getElementById("myModal").style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var captionText = document.getElementById("caption");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";

  }
  slides[slideIndex - 1].style.display = "block";

  captionText.innerHTML = this.alt;
}
.modal {
  width: 58%;
  height: 100%;
  top: 0;
  position: fixed;
  display: none;
  background-color: rgba(22, 22, 22, 0.5);
  margin-left: 300px;
  max-width: 779px;
  min-width: 779px;
}

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

.mySlides {
  display: none;
}

.close {
  position: relative;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
  left: 584px;
  top: 90px;
}

.close:hover,
.close:focus {
  color: #999;
  text-decoration: none;
  cursor: pointer;
}

.mySlides {
  display: none;
}

.cursor {
  cursor: pointer;
}

.cursor {
  cursor: pointer;
}

.prev {
  cursor: pointer;
  position: relative;
  top: -149px;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
  left: -10%;
}

.next {
  cursor: pointer;
  position: relative;
  top: -149px;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
  left: 600px;
}
<tr>
  <div class="row">
    <div class="column">
      <td>
        <p align="center"><img src="https://source.unsplash.com/collection/190727/1600x900" width="250" height="164" onclick="openModal();currentSlide(1)" class="hover-shadow cursor"></p>
      </td>
    </div>
    <div class="column">
      <td>
        <p align="center"><img src="https://source.unsplash.com/collection/190727/1600x910" width="250" height="164" onclick="openModal();currentSlide(2)" class="hover-shadow cursor"></p>
      </td>
    </div>
</tr>

<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close cursor" onclick="closeModal()">&times;</span>

    <div class="mySlides">

      <img src="https://source.unsplash.com/collection/190727/1600x900" style="width: 98%;
position: relative;
left: 10px;
top: 109px;">
      <p id="caption" style="padding-bottom: 7px;font-size: 17px;">Annual function</p>
    </div>

    <div class="mySlides">

      <img src="https://source.unsplash.com/collection/190727/1600x910" style="width: 98%;
position: relative;
left: 10px;
top: 109px;">
      <p id="caption" style="padding-bottom: 7px;font-size: 17px;">Annual function</p>
    </div>
    <a class="prev" id="prev1" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

【问题讨论】:

  • &lt;/div&gt;&lt;/tr&gt; 什么?没有&lt;table&gt;?没有&lt;td&gt;?内联onclick?为什么?此外,如果您确实想要 &lt;button&gt;,请不要使用锚元素。
  • 我发布短代码
  • 如果你问我,&lt;p&gt;短代码。不是一些损坏的不必要的标记。 &lt;div&gt; 不能是 &lt;tr&gt; 的直接子代 - 了解 HTML Table element 及其标记。
  • id="caption" 多次? ID 应该是唯一的。 developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
  • .mySlides?那是一张幻灯片——但那是“命名事物”的事情。内联style ...避免这种情况。使用样式表。

标签: javascript html css


【解决方案1】:
document.addEventListener('keydown', function(e) {
    if (e.keyCode === 37) {
        document.getElementById("prev1").click()
    } else if (e.keyCode === 39) {
        document.getElementById("next1").click()
    }
});

【讨论】:

  • 欢迎来到 StackOverflow。虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将提高​​答案的长期价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多