Search code examples
javascripthtmlcssimagegallery

How to Maintain Zoom on Image while Adding a Hyperlink


I would like to add a hyperlink to the images (so that it will open a larger version of the image) within the gallery while maintaining the 'zoom' effect on the thumbnail. I attempted to add a hyperlink to the first image but the zoom was no longer running. I apologise if my explanation isn't very good, I'm new to coding languages! Any help would be great!

Here is the link where I got the gallery code if it is any use to anyone! https://codepen.io/oknoblich/pen/ELfzd

< script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" > < /script> <
  script type = "text/javascript" >


  (function() {

    var documentElem = $(document),
      nav = $('nav'),
      lastScrollTop = 0;

    documentElem.on('scroll', function() {
      var currentScrollTop = $(this).scrollTop();

      //scroll down
      if (currentScrollTop > lastScrollTop) nav.addClass('hidden');
      //scroll up
      else nav.removeClass('hidden');

      lastScrollTop = currentScrollTop;
    });
  })();

<
/script>
<!-- End of Javascript for Navigation Menu -->
<style type="text/css">@import url('https://fonts.googleapis.com/css?family=Open+Sans:300');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.gallery {
  width: 640px;
  margin: 0 auto;
  padding: 5px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

.gallery>div {
  position: relative;
  float: left;
  padding: 5px;
}

.gallery>div>img {
  display: block;
  width: 200px;
  transition: .1s transform;
  transform: translateZ(0);
  /* hack */
}

.gallery>div:hover {
  z-index: 1;
}

.gallery>div:hover>img {
  transform: scale(1.1, 1.1);
  transition: .3s transform;
}

.cf:before,
.cf:after {
  display: table;
  content: "";
  line-height: 0;
}

.cf:after {
  clear: both;
}

h1 {
  margin: 40px 0;
  font-size: 30px;
  font-weight: 300;
  text-align: center;
}

</style>
<body>
  <!-- Start of Navigation Bar -->
  <nav>
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="about.html">About</a></li>
      <li id="current"><a href="gallery.html">Gallery</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
  <!-- End of Navigation Bar -->
  <br />
  <br />

  <h1> Gallery </h1>

  <!-- Stat of Gallery -->
  <div class="gallery cf">
    <!-- This is the image I tried to link but when I ran the code the zoom was no longer maintained. -->
    <div>
      <a href="Images/Optimized/FullImage_1.jpg"><img src="Images/Optimized/Thumbnail_1.jpg" width="200" height="300" alt="" /></a>
    </div>
    <div>
      <img src="Images/Optimized/Thumbnail_2.jpg" width="200" height="300" alt="" /> </div>
    <div>
      <img src="Images/Optimized/Thumbnail_3.jpg" width="200" height="300" alt="" /> </div>
    <div>
      <img src="Images/Optimized/Thumbnail_4.jpg" width="200" height="300" alt="" /> </div>
    <div>
      <img src="Images/Optimized/Thumbnail_5.jpg" width="200" height="300" alt="" /> </div>
    <div>
      <img src="Images/Optimized/Thumbnail_6.jpg" width="200" height="300" alt="" /> </div>
  </div>


Solution

  • Here is the code this will work for sure and will display image in large size.

    https://jsfiddle.net/fsfrkru0/

    HTML & CSS

    @import url('https://fonts.googleapis.com/css?family=Open+Sans:300');
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      font: 14px/1 'Open Sans', sans-serif;
      color: #555;
      background: #e5e5e5;
    }
    
    .gallery {
      width: 640px;
      margin: 0 auto;
      padding: 5px;
      background: #fff;
      box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
    }
    
    .gallery>div>a {
      position: relative;
      float: left;
      padding: 5px;
    }
    
    .gallery>div>a>img {
      display: block;
      width: 200px;
      transition: .1s transform;
      transform: translateZ(0);
      /* hack */
    }
    
    .gallery>div:hover>a {
      z-index: 1;
    }
    
    .gallery>div:hover>a>img {
      transform: scale(1.7, 1.7);
      transition: .3s transform;
    }
    
    .cf:before,
    .cf:after {
      display: table;
      content: "";
      line-height: 0;
    }
    
    .cf:after {
      clear: both;
    }
    
    h1 {
      margin: 40px 0;
      font-size: 30px;
      font-weight: 300;
      text-align: center;
    }
    <h1>Simple CSS Image Gallery with Zoom</h1>
    
    <div class="gallery cf">
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
      <div>
        <a href="http://abload.de/img/a6aawu.jpg" target="_blank"><img src="http://abload.de/img/a6aawu.jpg" styles="" /></a>
      </div>
    </div>

    Hope this helps.