1

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.

4

5 回答 5

6

这是一个非常简单的方法 - Darkbox Gallery的简化版本:

基本上,您将大图像存储src在一个data-pop属性中。单击此类元素
时会调用弹出窗口。 CSS 真的很简单,甚至 jQuery 也不应该很复杂——除了计算当前窗口大小的部分,以便定义弹出窗口的背景图像是 CSS3还是——其中“auto”用于较小的图像(所以他们不'不缩放):data-pop
containauto

/* 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>

于 2012-11-27T00:04:07.143 回答
1

有几个 jQuery 灯箱脚本可以帮助您做到这一点。

以下是一些最好的列表:http: //line25.com/articles/rounding-up-the-top-10-jquery-lightbox-scripts

于 2012-11-26T23:41:14.910 回答
1

像一个 jquery 灯箱?

http://line25.com/articles/rounding-up-the-top-10-jquery-lightbox-scripts

单击图像缩略图时,图像以模式加载。

于 2012-11-26T23:41:40.913 回答
1

灯箱。有 jQuery 插件。查看官方网站上的“如何使用”标签:http: //leandrovieira.com/projects/jquery/lightbox/

于 2012-11-26T23:42:35.690 回答
1

我认为facebox是一个不错的选择。他们在此页面中提供了您想要的功能示例。

于 2012-11-26T23:44:57.940 回答