增加前端放大镜
项目的产品页面是这样的
当点击这个页面时会跳转到一个页面
这个传值是通过简单的html传值实现的
<img src="" width="350"/>
"""
<script src="vendor/jquery.min.js" type="text/javascript"></script>
<script>
function getParam() {
var url = location.search;
var value = url.substring(7);
$('img').attr('src',value);
}
getParam()
</script>
使用了jquery的方法 attr 这个方法 是选择页面上的img标签,向img的src属性里添加value这个值。location.search
是获取url路径?后面的值。
之后是实现放大镜功能,完整代码
<style>
.nav-pills {
background-color: black;
}
*{
padding: 0;
margin: 0;
}
.box{
margin: 100px;
border: 1px solid #fff;
position: relative;
}
.small{
position: relative;
width: 350px;
height: 350px;
}
.mask{
width: 125px;
height: 125px;
background: rgba(255, 255, 0, 0.4);
position: absolute;
left: 0;
top: 0;
display: none;
cursor: move;
}
.big{
width: 350px;
height: 350px;
border: 1px solid #fff;
position: absolute;
top: 0;
left: 360px;
overflow: hidden;
display: none;
}
</style>
</head>
<script type="text/javascript">
window.onload = function(){
var box = document.getElementsByClassName("box")[0];
var small = box.children[0];
var big = box.children[1];
var bigImg = big.children[0];
var mask = small.children[1];
small.onmouseenter = function(){
big.style.display = "block";
mask.style.display = "block";
};
small.onmouseleave = function(){
big.style.display = "none";
mask.style.display = "none";
};
small.onmousemove = function(event){
event = event || window.event;
var pagex = event.pageX || scroll().left + event.clientX;
var pagey = event.pageY || scroll().top + event.clientY;
//减去mask宽高的一半,让鼠标在mask的中间
var x = pagex - box.offsetLeft - mask.offsetWidth/2;
var y = pagey - box.offsetTop - mask.offsetHeight/2;
//不让mask超出small
if(x < 0){
x=0;
}
if(x > small.offsetWidth - mask.offsetWidth){
x = small.offsetWidth - mask.offsetWidth;
}
if(y < 0){
y=0;
}
if(y > small.offsetHeight - mask.offsetHeight){
y = small.offsetHeight - mask.offsetHeight;
}
mask.style.left = x + "px";
mask.style.top = y + "px";
var scale = bigImg.offsetWidth / small.offsetWidth;
bigImg.style.marginLeft = -scale * x +"px";
bigImg.style.marginTop = -scale * y +"px";
}
};
</script>
<body>
<!--页头-->
<ul class="nav nav-pills">
<li role="presentation"><a href="">首页</a></li>
<li role="presentation"><a href="">产品信息</a></li>
<li role="presentation"><a href="">公司咨讯</a></li>
<li role="presentation"><a href="">公司简介</a></li>
</ul>
<div class="box">
<div class="small">
<img src="" width="350"/>
<div class="mask"></div>
</div>
<div class="big">
<img src=""/>
</div>
</div>
<script src="vendor/jquery.min.js" type="text/javascript"></script>
</body>
<script>
function getParam() {
var url = location.search;
var value = url.substring(7);
$('img').attr('src','../../'+value);
}
getParam()
</script>
最终效果
github地址:https://github.com/zhoushibo-111/SSMairuiqiwebsite
gitee地址:https://gitee.com/zhoushiwangchao/SSMairuiqiwebsite