Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

css - How to enlarge image from thumbnail in jQuery?

On my website there is a list of small thumbnails ("t1.jpg", "t2.jpg", "t3.jpg",...) and I want to create code that in centered dialog box will show a full-size version of the image. How to do this?

For example when I click on t1.jpg I want to see in centered box a big1.jpg image. Dynamically. I don't want to load all images when the page is loading. It must be good performance for that.

Anyone can give me some advice? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a really simple approach - a simplified version of Darkbox Gallery:

Basically you store the large image src in a data-pop attribute.
The popup is called when such data-pop element is clicked.
The CSS is really straightforward and even the jQuery should not be much complicated - beside the part that calculates the current window size in order to define should the popup's background image be CSS3 contain or auto - where "auto" is used for smaller images (so they don't scale):

/* POP BOX */
;(function() {

  var $pop = $("#pop");

  $(document).on("click", "[data-pop]", function() {

    var popSrc= $(this).data("pop"),
        docW= Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
        docH= Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

    $pop.addClass("visible loading").css({backgroundImage:"none"});

    $("<img/>").on("load", function() {
      var bigger = (this.width > docW || this.height > docH)
      $pop.removeClass("loading").css({
        backgroundSize: bigger ? "contain" : "auto",
        backgroundImage: "url(" + this.src + ")"
      });
    }).attr("src", popSrc);

  });

  $("#popClose").on("click", function() {
    $pop.removeClass("visible loading");
  });

}());
/* 
POP BOX 
*/
#pop {
  position: fixed;
  z-index: 999999;
  color: #fff;
  background: rgba(0, 0, 0, 0.6) none no-repeat 50% 50%;
  box-shadow: 0 0 0 24px rgba(0, 0, 0, 0.6);
  left: 24px;
  top: 24px;
  right: 24px;
  bottom: 24px;
  transition: 0.3s;
  visibility: hidden;
  opacity: 0;
}

#pop.loading:after {
  content: "Loading...";
  position: absolute;
  top: 50%;
  left: 44%;
}

#pop.visible {
  visibility: visible;
  opacity: 1;
}

#popClose {
  cursor: pointer;
  position: absolute;
  top: 0px;
  right: 0px;
  font-size: 2em;
}
<!-- POP BOX -->
<div id="pop"><a id="popClose">&times;</a></div>

<!-- USE EXAMPLE -->
<img src="http://placehold.it/50x50/0bf" data-pop="http://placehold.it/860x590/0bf" alt="" />
<img src="http://placehold.it/50x50/f0b" data-pop="http://placehold.it/590x860/f0b" alt="" />



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...