jQuery點選小圖彈出大圖

antzone發表於2018-05-31

這樣的效果在實際應用中比較常見,點選小圖可以彈出一個更加醒目的大圖。

這裡為了掩飾方便使用了一張圖片,實際應用應該是一個縮圖和一個完整大圖。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  padding: 0;
  margin: 0;
}
.smallImg {
  max-width: 250px;
  height: 150px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -125px;
  margin-top: -75px;
  cursor: pointer;
}
.smallImg img {
  display: block;
  width: 100%;
  height: 100%;
}
.mark {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,.5);
  z-index: 10;
  display: none;
}
.bigImg {
  position: fixed;
  top: 50%;
  left: 50%;
  border-radius: 4px;
}
.animation {
  -webkit-animation: bounceIn 1s .2s ease both;
  -moz-animation: bounceIn 1s .2s ease both;
}
@-webkit-keyframes bounceIn {
  0% {
    opacity: 0;
    -webkit-transform: scale(.3);
  }
  50% {
    opacity: 1;
    -webkit-transform: scale(1.05);
  }
  70% {
    -webkit-transform: scale(.9);
  }
  100% {
    -webkit-transform: scale(1);
  }
}
@-moz-keyframes bounceIn {
  0% {
    opacity: 0;
    -moz-transform: scale(.3);
  }
  50% {
    opacity: 1;
    -moz-transform: scale(1.05);
  }
  70% {
    -moz-transform: scale(.9);
  }
  100% {
    -moz-transform: scale(1);
  }
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function () {
  $(".smallImg").click(function (e) {
    $(".mark").show();
 
    var imgSrc = $(".smallImg img").attr("src");
    $(".bigImg").append("<img src=" + imgSrc + " class=" + 'bigImg-cont' + ">");
    $(".bigImg").addClass('animation');
    var imgW = -parseInt($(".bigImg-cont").width() / 2);
    var imgH = -parseInt($(".bigImg-cont").height() / 2);
 
    $(".bigImg").css("marginLeft", imgW);
    $(".bigImg").css("marginTop", imgH);
    event.stopPropagation();
  });
 
  $(document).click(function () {
    $(".mark").hide();
    $(".bigImg").html('');
    $(".bigImg").removeClass('animation');
  });
 
  $(".bigImg").click(function (event) {
    event.stopPropagation();
  });
})
</script>
</head>
<body>
  <div class="smallImg">
    <img src="demo/CSS/img/lianmeng.jpg" alt="">
  </div>
  <div class="mark">
    <div class="bigImg">
    </div>
  </div>
</body>
</html>

相關文章