Javascript响应图像库缩略图

时间:2017-06-26 18:42:51

标签: javascript jquery html css

我需要做一个响应式图片缩略图库,我现在看起来像你可以在代码段中看到的那个。

由于我必须显示30张图片,有没有办法可以让我的代码更有效率,以便我不必一直重复图像?

不幸的是我无法使用插件,但我可以自由使用jQuery。

You can check my pen here

我的html和css看起来像这样:

HTML:

<h2 style="text-align:center">Lightbox</h2>

<div class="row">
  <div class="column">
    <img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg " style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
  </div>
</div>

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

    <div class="mySlides">
      <div class="numbertext">1 / 4</div>
      <img src="https://placehold.it/180x180.jpg " style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 4</div>
      <img src="https://placehold.it/180x180.jpg " style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 4</div>
      <img src="https://placehold.it/180x180.jpg " style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">4 / 4</div>
      <img src="https://placehold.it/180x180.jpg " style="width:100%">
    </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption"></p>
    </div>


    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg " style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>

CSS:

body {
  font-family: Verdana, sans-serif;
  margin: 0;
}

* {
  box-sizing: border-box;
}

.row > .column {
  padding: 0 8px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.column {
  float: left;
  width: 25%;
}

/* The Modal (background) */
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  width: 90%;
  max-width: 1200px;
}

/* The Close Button */
.close {
  color: white;
  position: absolute;
  top: 10px;
  right: 25px;
  font-size: 35px;
  font-weight: bold;
}

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

.mySlides {
  display: none;
}

.cursor {
  cursor: pointer
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

img {
  margin-bottom: -4px;
}

.caption-container {
  text-align: center;
  background-color: black;
  padding: 2px 16px;
  color: white;
}

.demo {
  opacity: 0.6;
}

.active,
.demo:hover {
  opacity: 1;
}

img.hover-shadow {
  transition: 0.3s
}

.hover-shadow:hover {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}

1 个答案:

答案 0 :(得分:0)

您将不得不以某种方式拥有图像元素,因此不应该成为您的焦点。但是,您可以采取其他措施来减少冗余,分离内容,表示和行为,并随着时间的推移为您提供更好的扩展和调试页面的方法。

首先,使用CSS选择器来减少冗余并将内容与演示文稿分开,而不是将内联样式应用于元素。

此外,虽然静态HTML没有使用任何JavaScript运行时的强大功能,但您可以自动执行这些类的应用程序以及设置点击事件处理程序(这不应该是&#39;无论如何,使用JavaScript完成内联HTML事件处理属性。这不会消除所有图像元素,但会减少该代码的冗余。

其他一些事情......您的某些HTML无效,因为您的某些图片元素没有必要的alt属性来为使用屏幕阅读器软件的用户提供替代文字说明否则无法看到图像。

最后,看起来您的许多图像路径都有一个尾随空格作为属性值的一部分。

&#13;
&#13;
// Place all of this code into a <script></script> element and place that element 
// just before the closing of the <body> element (</body>) so that by the time
// the script is reached, all the HTML will have already been parsed into memory.

// Get references to the elements you need to work with:

var prev = document.querySelector(".prev");
var next = document.querySelector(".next");
var close = document.querySelector(".close");

// Get a node list of the sets of images and turn the node list into a proper array
var columnImages1 = Array.prototype.slice.call(document.querySelectorAll("div.row > div.colum > img.hover-shadow.cursor"));

var columnImages2 = Array.prototype.slice.call(document.querySelectorAll("div.column > img.demo"));

// Loop over the first group of images and set up their click event handlers
columnImages1.forEach(function(img, index){
  img.addEventListener("click", function(){
    openModal();
    currentSlide(index + 1);
  });
});

// Loop over the second group of images and set up their click event handlers
columnImages1.forEach(function(img, index){
  img.addEventListener("click", function(){
    currentSlide(index + 1);
  });
});

// Set up the click event handler for the close, previous and next buttons
close.addEventListener("click", closeModal);
prev.addEventListener("click", function(){ plusSlides(-1) });
next.addEventListener("click", function(){ plusSlides(1) });
&#13;
* { box-sizing: border-box; }

body {
  font-family: Verdana, sans-serif;
  margin: 0;
}

/* This will eliminate the need to repeat "width:100%" on all your slides */
.mySlides > img, .column > img { width: 100%; }


.row > .column {
  padding: 0 8px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.column {
  float: left;
  width: 25%;
}

/* The Modal (background) */
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;
}
&#13;
<h2 style="text-align:center">Lightbox</h2>

<div class="row">
  <div class="column">
    <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" 
         class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" 
         class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" 
         class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="https://placehold.it/180x180.jpg" alt="You need a short description here" 
         class="hover-shadow cursor">
  </div>
</div>

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

    <div class="mySlides">
      <div class="numbertext">1 / 4</div>
      <img src="https://placehold.it/180x180.jpg" alt="You need a short description here">
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 4</div>
      <img src="https://placehold.it/180x180.jpg" alt="You need a short description here">
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 4</div>
      <img src="https://placehold.it/180x180.jpg" alt="You need a short description here">
    </div>

    <div class="mySlides">
      <div class="numbertext">4 / 4</div>
      <img src="https://placehold.it/180x180.jpg" alt="You need a short description here">
    </div>

    <a class="prev">&#10094;</a>
    <a class="next">&#10095;</a>

    <div class="caption-container">
      <p id="caption"></p>
    </div>


    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Trolltunga, Norway">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo cursor" src="https://placehold.it/180x180.jpg" alt="Northern Lights">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;