【前端】特效-Javascript实现购物页面图片放大效果
实现效果
实现代码:
<!DOCTYPE html>
<html>
<head>
<title>购物图片放大</title>
<meta charset="utf-8">
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.box{
width: 200px;
height: 200px;
margin: 100px;
border: 1px solid #ccc;
position: relative;
}
.big{
width: 400px;
height: 400px;
position: absolute;
top: 0;
left: 360px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
}
.mask{
width: 80px;
height: 80px;
background-color: rgba(3,12,60,0.4);
position: absolute;
top: 0;
left: 0;
cursor: move;
display: none;
}
.small{
position: relative;
}
.small img{
width: 200px;
height: 200px;
}
.big img{
height: 1000px;
width: 1000px;
}
img{
vertical-align: top;
}
</style>
</head>
<body>
<!-- 大图片宽度/big框宽度 = small框宽度/mask框宽度,并且小照片宽度 = small框宽度,如BigImg = 1000, SmallImg = small = 200,big=400,那么mask = 80 -->
<div class="box">
<div class="small">
<img src="./resident.jpg" alt="small Image">
<div class="mask"></div>
</div>
<div class="big">
<img src="./resident.jpg" alt="Big Image">
</div>
</div> <script type="text/javascript">
window.onload = function(){
// 鼠标放到小盒子上时,大盒子图片同等比例移动
//技术点:onmouseenter==onmouseover 第一个不冒泡
//技术点:onmouseleave==onmouseout 第一个不冒泡
//步骤:
//1.鼠标放上去显示盒子,移开隐藏盒子
//2.mask跟随移动
//3.右侧的大图片,等比例移动 var box = document.getElementsByClassName("box")[0];
var small = box.firstElementChild || box.firstChild;
var big = box.children[1];
var mask = small.children[1];
var bigImg = big.children[0]; // 1.鼠标放上去显示盒子,移开隐藏盒子(为小盒子绑定事件) // 调用封装好的方法,显示元素
small.onmouseenter = function(){
show(mask);
show(big);
}
// 调用封装好的方法,隐藏元素
small.onmouseleave = function(){
hide(mask);
hide(big);
} // 2. mask跟随鼠标移动
// 绑定事件是onmousemove,事件源是small,只要在小盒子上移动1px,mask也要跟随移动
small.onmousemove = function(event){
// 想移动mask,需要知道鼠标在small中的位置,x作为mask的left值,y作为mask的top值
event = event || window.event;
// 获取鼠标在整个页面的位置
var pagex = event.pageX || scroll().left + event.clientX;
var pagey = event.pageY || scroll().top + event.clientY;
// 让鼠标在mask的最中间,减去mask宽高的一半,x、y为mask的坐标
// console.log(pagex + " " + pagey);
var x = pagex - box.offsetLeft - mask.offsetWidth/2;
var y = pagey - box.offsetTop - mask.offsetHeight/2;
// 限制mask的范围,left取值大于0,小于小盒子的宽减mask的宽
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
// console.log("x:" + x + " y:" + y);
mask.style.left = x + "px";
mask.style.top = y + "px"; //3.右侧的大图片,等比例移动
// 大图片/大盒子 = 小图片/mask盒子
// 大图片走的距离/mask走的距离 = (大图片-大盒子)/(小图片-mask)
//比例var times = (bigImg.offsetWidth-big.offsetWidth)/(small.offsetWidth-mask.offsetWidth);
//大图片走的距离/mask盒子走的距离 = 大图片/小图片
var times = bigImg.offsetWidth/small.offsetWidth;
var _x = x * times;
var _y = y * times; bigImg.style.marginLeft = -_x + "px";
bigImg.style.marginTop = -_y + "px";
}
}
// 显示隐藏元素
function show(element){
element.style.display = "block";
}
function hide(element){
element.style.display = "none";
}
</script>
</body>
</html>
【前端】特效-Javascript实现购物页面图片放大效果的更多相关文章
- jquery图片放大插件鼠标悬停图片放大效果
都知道jquery都插件是非常强大的,最近分享点jquery插件效果,方便效果开发使用. 一.HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- 为BlueLake主题增加图片放大效果
fancyBox 是一个流行的媒体展示增强组件,可以方便为网站添加图片放大.相册浏览.视频弹出层播放等效果.优点有使用简单,支持高度自定义,兼顾触屏.响应式移动端特性,总之使用体验相当好. 现在,我们 ...
- js图片放大效果
实现购物网站里的图片放大效果,jqzoom很好用.今天才接触它,很快就上手了.看了一个示例,在放大图像上方貌似有水印,后经排查,原来是图片的标题,然后设置title为false,搞定.
- magento中如何实现产品图片放大效果
Magento列表页用jQuery实现产品图片放大效果今天看到个网站,鼠标移到列表页的产品图片上,旁边会弹出一个大图,感觉不错,就自己在Magento里写了个.先看看效果 这个效果比较好实现,打开li ...
- 浅谈CSS和JQuery实现鼠标悬浮图片放大效果
对于刚刚学习网页前台设计的同学一定对图片的处理非常苦恼,那么这里简单的讲解一下几个图片处理的实例. 以.net为平台,微软的Visual Studio 2013为开发工具,当然前台技术还是采用CSS3 ...
- (JS+CSS)实现图片放大效果
代码很简单,在这里就不过多阐述,先上示例图: 实现过程: html部分代码很简单 <div id="outer"> <p>点击图片</p> &l ...
- jQuery实现网站图片放大效果
实现效果:当鼠标指向商品图片时,图片会自动放大. <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- [原创]实现android知乎、一览等的开场动画图片放大效果
代码下载地址: https://github.com/Carbs0126/AutoZoomInImageView 知乎等app的开场动画为:一张图片被显示到屏幕的正中央,并充满整个屏幕,过一小段时间后 ...
- iOSUITableView头部带有图片并且下拉图片放大效果
最近感觉UITableview头部带有图片,并且下拉时图片放大这种效果非常炫酷,所以动手实现了一下,效果如下图: 1.gif 实现原理很简单,就是在UITableview上边添加一个图片子视图,在ta ...
随机推荐
- linux 允许mysql用户远程访问
搭建服务器..怎么导入数据库? 直接来个用户吧 数据库名字(已存在): table 创建mysql新用户,并指定数据库,允许远程访问 mysql用户: test mysql用户密码: test666 ...
- postgres模板数据库
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的.缺省时,它拷贝名为 template1 的标准系统数据库.所以该数据库是创建新数据库的"模板".如果你给 ...
- LeetCode——Best Time to Buy and Sell Stock III
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- click() bind() live() delegate()区别
click(),bind(),live()都是执行事件时使用的方法 1.click()单击事件方法: $("a").click(function() { alert("h ...
- SDOI 2016 Round1 Day2
生成魔咒 /* 后缀数组+双向链表 参照:https://blog.csdn.net/clove_unique/article/details/53911757 */ #include<cstd ...
- shell脚本关闭tomcat
使用shell脚本快速关闭tomcat,就是获取tomcat进程后,一起kill掉: #!/bin/sh #kill tomcat pid name=tomcat-emall pidlist=`ps ...
- 170614、MySQL存储引擎-MyISAM与InnoDB区别
MyISAM 和InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理 ...
- pta 习题集 5-15 数组循环左移
本题要求实现一个对数组进行循环左移的简单函数:一个数组aa中存有nn(>0>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向左移mm(≥0≥0)个位置,即将aa中的数据由(a0a ...
- Golang学习 - builtin 包
Go builtin包提供了go预先声明的函数.变量等的文档.这些函数变量等的实现其实并不是在builtin包里,只是为了方便文档组织. 这些内置的变量.函数.类型无需引入包即可使用. 默认提供的有: ...
- sql server 碎片整理——DBCC SHOWCONTIG
转自: 1.http://blog.sina.com.cn/s/blog_6d2675450101ks6i.html 2.http://www.cnblogs.com/CareySon/archive ...