图片点击放大 效果 借鉴了下面这个
这里这里,点击图片放大功能
本案例需要使用jQuery
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>易租康适老化改造</title>
</head>
<body>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #F4F4F4;
}
.box .header_topBox {
width: 100%;
height: 100%;
}
.header_topBox .imageBanner {
width: 100%;
}
.box .contentBox {
width: 95%;
margin: 15px auto;
padding-bottom: 100px;
}
.contentBox .titleBox {
background-color: #fff !important;
border-radius: 5px;
padding: 10px;
font-size: 24px;
}
/* 基础资料 */
.contentBox .dataBox {
margin-top: 20px;
}
.dataBox .dataBox_titles {
background-color: #2A9BFC;
border-radius: 2px;
height: 60px;
font-size: 24px;
color: #fff;
padding: 10px 25px;
}
.dataBox .videBox{
margin-top: 10px;
width: 100%;
}
.dataBox .videBox .videos{
width: 100%;
}
.dataBox .dataBox_informationBox {
background-color: #fff !important;
}
.dataBox_informationBox .dataBox_information {
font-size: 24px;
padding: 15px 10px;
border-bottom: 1px solid #F4F4F4;
}
.dataBox_informationBox .dataBox_information:last-child {
border-bottom: none;
}
/* 改造成果 */
.dataBox .dataBox_titlesBox{
height: 60px;
line-height: 60px;
padding: 8px 25px;
background-color: #2A9BFC;
border-radius: 2px;
font-size: 24px;
color: #fff;
display: flex;
justify-content: space-between;
align-items: center;
}
.dataBox_btn {
margin-right: 30px;
}
.dataBox_titlesBox .dataBox_btn .rightBtn,.bottomBtn{
width: 20px;
height: 20px;
}
.dataBox .Transformation_Box{
background-color: #fff;
}
.Transformation_Box .Transformation{
width: 97%;
margin: 0 auto;
padding: 15px 0 15px 0;
}
.Transformation_Box .Transformation .Transformation_Title{
background-color: #2A9BFC;
border-radius: 2px;
font-size: 24px;
color: #fff;
padding: 10px 25px;
}
.Transformation_Box .Transformation .Transformatio_Content{
display: flex;
justify-content: space-around;
font-size: 24px;
color: #3DA5FD;
padding: 15px 0;
/* border-top: 1px solid #F4F4F4; */
text-align: center;
}
.Transformation_Box .Transformation .Transformatio_Content .Transformatio_Content_left,.Transformatio_Content_right{
width: 45%;
}
.Transformation_Box .Transformation .Transformatio_Content .Transformatio_Content_left img,.Transformatio_Content_right img{
margin-top: 20px;
width: 100%;
height: auto;
}
@media screen and (max-width: 375px) {
.contentBox .titleBox {
font-size: 20px;
}
.dataBox .dataBox_titles {
background-color: #2A9BFC;
border-radius: 2px;
height: 50px;
font-size: 20px;
color: #fff;
padding: 10px 25px;
}
.dataBox_informationBox .dataBox_information {
font-size: 20px;
padding: 15px 10px;
border-bottom: 1px solid #F4F4F4;
}
/* 改造成果 */
.dataBox .dataBox_titlesBox{
font-size: 20px;
}
.Transformation_Box .Transformation .Transformation_Title{
font-size: 20px;
}
.Transformation_Box .Transformation .Transformatio_Content{
font-size: 20px;
}
}
</style>
<div class="box">
<div class="header_topBox">
<img src="https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_960_720.jpg" class="imageBanner">
</div>
<div class="contentBox">
<div class="titleBox">
<div class="title">
我就是标题
我就是标题
我就是标题
我就是标题
我就是标题
我就是标题
我就是标题我就是标题我就是标题
</div>
</div>
<!-- 基础资料 -->
<div class="dataBox">
<div class="dataBox_titles">
基础资料
</div>
<div class="dataBox_informationBox">
<div class="dataBox_information">
姓名 : 瑶瑶
</div>
<div class="dataBox_information">
性别 : 女
</div>
<div class="dataBox_information">
居住地址 : 爪哇果
</div>
</div>
</div>
<!-- 改造成果 -->
<div class="dataBox">
<div class="dataBox_titlesBox">
<div class="dataBox_title">
改造成果
</div>
<div class="dataBox_btn">
<img src="img/bottom.png" class="bottomBtn" onclick="showReform()">
<img src="img/right.png" class="rightBtn" onclick="hideReform()">
</div>
</div>
<div class="Transformation_Box">
<div class="Transformation">
<div class="Transformation_Title">
改造前后对比
</div>
<div class="Transformatio_Content">
<div class="Transformatio_Content_left">
改造前
<div class="imgBox">
<img src="https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_960_720.jpg" width="100" class="pic" >
</div>
</div>
<div class="Transformatio_Content_right">
改造后
<div class="imgBox">
<img src="https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_960_720.jpg" width="100" class="pic" >
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 改造前视频 -->
<div class="dataBox">
<div class="dataBox_titles">
改造前视频
</div>
<div class="videBox">
<video src="movie.ogg" controls="controls" class="videos">
您的浏览器不支持 video 标签。
</video>
</div>
</div>
<!-- 改造后视频 -->
<div class="dataBox">
<div class="dataBox_titles">
改造后视频
</div>
<div class="videBox">
<video src="movie.ogg" controls="controls" class="videos">
您的浏览器不支持 video 标签。
</video>
</div>
</div>
</div>
<div id="outerdiv" style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;">
<div id="innerdiv" style="position:absolute;">
<img id="bigimg" style="border:5px solid #fff;" src="" />
</div>
</body>
</div>
<script src="./js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(".rightBtn,.Transformation_Box").css({"display":"none"})
function showReform(){
$(".bottomBtn").css({"display":"none"})
$(".rightBtn,.Transformation_Box").css({"display":"block"})
}
function hideReform(){
$(".bottomBtn").css({"display":"block"})
$(".rightBtn,.Transformation_Box").css({"display":"none"})
}
$(function(){
$(".pic").click(function(){
var _this = $(this);//将当前的pimg元素作为_this传入函数
imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
});
});
function imgShow(outerdiv, innerdiv, bigimg, _this){
var src = _this.attr("src");//获取当前点击的pimg元素中的src属性
$(bigimg).attr("src", src);//设置#bigimg元素的src属性
/*获取当前点击图片的真实大小,并显示弹出层及大图*/
$("<img/>").attr("src", src).load(function(){
var windowW = $(window).width();//获取当前窗口宽度
var windowH = $(window).height();//获取当前窗口高度
var realWidth = this.width;//获取图片真实宽度
var realHeight = this.height;//获取图片真实高度
var imgWidth, imgHeight;
var scale = 0.8;//缩放尺寸,当图片真实宽度和高度大于窗口宽度和高度时进行缩放
if(realHeight>windowH*scale) {//判断图片高度
imgHeight = windowH*scale;//如大于窗口高度,图片高度进行缩放
imgWidth = imgHeight/realHeight*realWidth;//等比例缩放宽度
if(imgWidth>windowW*scale) {//如宽度扔大于窗口宽度
imgWidth = windowW*scale;//再对宽度进行缩放
}
} else if(realWidth>windowW*scale) {//如图片高度合适,判断图片宽度
imgWidth = windowW*scale;//如大于窗口宽度,图片宽度进行缩放
imgHeight = imgWidth/realWidth*realHeight;//等比例缩放高度
} else {//如果图片真实高度和宽度都符合要求,高宽不变
imgWidth = realWidth;
imgHeight = realHeight;
}
$(bigimg).css("width",imgWidth);//以最终的宽度对图片缩放
var w = (windowW-imgWidth)/2;//计算图片与窗口左边距
var h = (windowH-imgHeight)/2;//计算图片与窗口上边距
$(innerdiv).css({"top":h, "left":w});//设置#innerdiv的top和left属性
$(outerdiv).fadeIn("fast");//淡入显示#outerdiv及.pimg
});
$(outerdiv).click(function(){//再次点击淡出消失弹出层
$(this).fadeOut("fast");
});
}
</script>
</body>
</html>
效果图: