JS相册图片抖动放大展示效果的示例代码


Posted in Javascript onJanuary 29, 2021

上篇文章给大家介绍了JS实现简单抖动效果,感兴趣的朋友点击查看。

今天给大家分享JS相册图片抖动放大展示效果,效果图如下所示:

JS相册图片抖动放大展示效果的示例代码

var xm;
var ym;
 
/* ==== onmousemove event ==== */
document.onmousemove = function(e){
	if(window.event) e=window.event;
	xm = (e.x || e.clientX);
	ym = (e.y || e.clientY);
}
 
/* ==== window resize ==== */
function resize() {
	if(diapo)diapo.resize();
}
onresize = resize;
 
/* ==== opacity ==== */
setOpacity = function(o, alpha){
	if(o.filters)o.filters.alpha.opacity = alpha * 100; else o.style.opacity = alpha;
}
 
 
 
/* ===== encapsulate script ==== */
diapo = {
	O : [],
	DC : 0,
	img : 0,
	txt : 0,
	N : 0,
	xm : 0,
	ym : 0,
	nx : 0,
	ny : 0,
	nw : 0,
	nh : 0,
	rs : 0,
	rsB : 0,
	zo : 0,
	tx_pos : 0,
	tx_var : 0,
	tx_target : 0,
 
	/// script parameters 
	attraction : 2,
	acceleration : .9,
	dampening : .1,
	zoomOver : 2,
	zoomClick : 6,
	transparency : .8,
	font_size: 18,
	//
 
	/* ==== diapo resize ==== */
	resize : function(){
		with(this){
			nx = DC.offsetLeft;
			ny = DC.offsetTop;
			nw = DC.offsetWidth;
			nh = DC.offsetHeight;
			txt.style.fontSize = Math.round(nh / font_size) + "px";
			if(Math.abs(rs-rsB)<100) for(var i=0; i<N; i++) O[i].resize();
			rsB = rs;
		}
	},
 
	/* ==== create diapo ==== */
	CDiapo : function(o){
		/* ==== init variables ==== */
		this.o    = o;
		this.x_pos  = this.y_pos  = 0;
		this.x_origin = this.y_origin = 0;
		this.x_var  = this.y_var  = 0;
		this.x_target = this.y_target = 0;
		this.w_pos  = this.h_pos  = 0;
		this.w_origin = this.h_origin = 0;
		this.w_var  = this.h_var  = 0;
		this.w_target = this.h_target = 0;
		this.over   = false;
		this.click  = false;
 
		/* ==== create shadow ==== */
		this.spa = document.createElement("span");
		this.spa.className = "spaDC";
		diapo.DC.appendChild(this.spa);
 
		/* ==== create thumbnail image ==== */
		this.img = document.createElement("img");
		this.img.className = "imgDC";
		this.img.src = o.src;
		this.img.O = this;
		diapo.DC.appendChild(this.img);
		setOpacity(this.img, diapo.transparency);
 
		/* ==== mouse events ==== */
		this.img.onselectstart = new Function("return false;");
		this.img.ondrag = new Function("return false;");
		this.img.onmouseover = function(){
			diapo.tx_target=0;
			diapo.txt.innerHTML=this.O.o.alt;
			this.O.over=true;
			setOpacity(this,this.O.click?diapo.transparency:1);
		}
		this.img.onmouseout = function(){
			diapo.tx_target=-diapo.nw;
			this.O.over=false;
			setOpacity(this,diapo.transparency);
		}
		this.img.onclick = function() {
			if(!this.O.click){
				if(diapo.zo && diapo.zo != this) diapo.zo.onclick();
				this.O.click = true;
				this.O.x_origin = (diapo.nw - (this.O.w_origin * diapo.zoomClick)) / 2;
				this.O.y_origin = (diapo.nh - (this.O.h_origin * diapo.zoomClick)) / 2;
				diapo.zo = this;
				setOpacity(this,diapo.transparency);
			} else {
				this.O.click = false;
				this.O.over = false;
				this.O.resize();
				diapo.zo = 0;
			}
		}
 
		/* ==== rearrange thumbnails based on "imgsrc" images position ==== */
		this.resize = function (){
			with (this) {
				x_origin = o.offsetLeft;
				y_origin = o.offsetTop;
				w_origin = o.offsetWidth;
				h_origin = o.offsetHeight;
			}
		}
 
		/* ==== animation function ==== */
		this.position = function (){
			with (this) {
				/* ==== set target position ==== */
				w_target = w_origin;
				h_target = h_origin;
				if(over){
					/* ==== mouse over ==== */
					w_target = w_origin * diapo.zoomOver;
					h_target = h_origin * diapo.zoomOver;
					x_target = diapo.xm - w_pos / 2 - (diapo.xm - (x_origin + w_pos / 2)) / (diapo.attraction*(click?10:1));
					y_target = diapo.ym - h_pos / 2 - (diapo.ym - (y_origin + h_pos / 2)) / (diapo.attraction*(click?10:1));
				} else {
					/* ==== mouse out ==== */
					x_target = x_origin;
					y_target = y_origin;
				}
				if(click){
					/* ==== clicked ==== */
					w_target = w_origin * diapo.zoomClick;
					h_target = h_origin * diapo.zoomClick;
				}
 
				/* ==== magic spring equations ==== */
				x_pos += x_var = x_var * diapo.acceleration + (x_target - x_pos) * diapo.dampening;
				y_pos += y_var = y_var * diapo.acceleration + (y_target - y_pos) * diapo.dampening;
				w_pos += w_var = w_var * (diapo.acceleration * .5) + (w_target - w_pos) * (diapo.dampening * .5);
				h_pos += h_var = h_var * (diapo.acceleration * .5) + (h_target - h_pos) * (diapo.dampening * .5);
				diapo.rs += (Math.abs(x_var) + Math.abs(y_var));
 
				/* ==== html animation ==== */
				with(img.style){
					left  = Math.round(x_pos) + "px";
					top  = Math.round(y_pos) + "px";
					width = Math.round(Math.max(0, w_pos)) + "px";
					height = Math.round(Math.max(0, h_pos)) + "px";
					zIndex = Math.round(w_pos);
				}
				with(spa.style){
					left  = Math.round(x_pos + w_pos * .1) + "px";
					top  = Math.round(y_pos + h_pos * .1) + "px";
					width = Math.round(Math.max(0, w_pos * 1.1)) + "px";
					height = Math.round(Math.max(0, h_pos * 1.1)) + "px";
					zIndex = Math.round(w_pos);
				}
			}
		}
	},
 
	/* ==== main loop ==== */
	run : function(){
		diapo.xm = xm - diapo.nx;
		diapo.ym = ym - diapo.ny;
		/* ==== caption anim ==== */
		diapo.tx_pos += diapo.tx_var = diapo.tx_var * .9 + (diapo.tx_target - diapo.tx_pos) * .02;
		diapo.txt.style.left = Math.round(diapo.tx_pos) + "px";
		/* ==== images anim ==== */
		for(var i in diapo.O) diapo.O[i].position();
		/* ==== loop ==== */
		setTimeout("diapo.run();", 16);
	},
 
	/* ==== load images ==== */
	images_load : function(){
		// ===== loop until all images are loaded =====
		var M = 0;
		for(var i=0; i<diapo.N; i++) {
			if(diapo.img[i].complete) {
				diapo.img[i].style.position = "relative";
				diapo.O[i].img.style.visibility = "visible";
				diapo.O[i].spa.style.visibility = "visible";
				M++;
			}
			resize();
		}
		if(M<diapo.N) setTimeout("diapo.images_load();", 128);
	},
 
	/* ==== init script ==== */
	init : function() {
		diapo.DC = document.getElementById("diapoContainer");
		diapo.img = diapo.DC.getElementsByTagName("img");
		diapo.txt = document.getElementById("caption");
		diapo.N = diapo.img.length;
		for(i=0; i<diapo.N; i++) diapo.O.push(new diapo.CDiapo(diapo.img[i]));
		diapo.resize();
		diapo.tx_pos = -diapo.nw;
		diapo.tx_target = -diapo.nw;
		diapo.images_load();
		diapo.run();
	}
}
 
/* ==== start script ==== */
function dom_onload() {
	if(document.getElementById("diapoContainer")) diapo.init(); else setTimeout("dom_onload();", 128);
}
dom_onload();

到此这篇关于JS相册图片抖动放大展示效果的示例代码的文章就介绍到这了,更多相关js图片放大抖动内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
浅谈jQuery事件绑定原理
Jan 02 Javascript
jQuery实现的动态伸缩导航菜单实例
May 07 Javascript
Bootstrap每天必学之表格
Nov 23 Javascript
Bootstrap表格和栅格分页实例详解
May 20 Javascript
JavaScript中的对象继承关系
Aug 01 Javascript
Bootstrap table表格简单操作
Feb 07 Javascript
js控制文本框禁止输入特殊字符详解
Apr 07 Javascript
JavaScript使用ZeroClipboard操作剪切板
May 10 Javascript
vue 开发一个按钮组件的示例代码
Mar 27 Javascript
Node.js利用console输出日志文件的方法示例
Apr 27 Javascript
Javascript基于OOP实实现探测器功能代码实例
Aug 26 Javascript
解决Antd 里面的select 选择框联动触发的问题
Oct 24 Javascript
vue监听键盘事件的相关总结
Jan 29 #Vue.js
javascript实现拼图游戏
Jan 29 #Javascript
JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法
Jan 29 #Javascript
Javascript实现打鼓效果
Jan 29 #Javascript
JS实现点击掉落特效
Jan 29 #Javascript
Javascript实现关闭广告效果
Jan 29 #Javascript
Vue 实例中使用$refs的注意事项
Jan 29 #Vue.js
You might like
php 创建以UNIX时间戳命名的文件夹(示例代码)
2014/03/08 PHP
php字符串操作常见问题小结
2016/10/11 PHP
thinkphp jquery实现图片上传和预览效果
2020/07/22 PHP
thinkPHP中_initialize方法实例分析
2016/12/05 PHP
jQuery 添加/移除CSS类实现代码
2010/02/11 Javascript
为jQuery.Treeview添加右键菜单的实现代码
2010/10/22 Javascript
通过jQuery源码学习javascript(一)
2012/12/27 Javascript
推荐一个封装好的getElementsByClassName方法
2014/12/02 Javascript
了不起的node.js读书笔记之例程分析
2014/12/22 Javascript
微信小程序教程系列之视图层的条件渲染(10)
2017/04/19 Javascript
AngularJS中ng-class用法实例分析
2017/07/06 Javascript
js中null与空字符串&quot;&quot;的区别讲解
2019/01/17 Javascript
在vue项目中使用Jquery-contextmenu插件的步骤讲解
2019/01/27 jQuery
详解JavaScript实现动态的轮播图效果
2019/04/29 Javascript
js实现自定义右键菜单
2020/05/18 Javascript
[35:26]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第三局
2016/02/26 DOTA
python 实现网上商城,转账,存取款等功能的信用卡系统
2016/07/15 Python
基于Pandas读取csv文件Error的总结
2018/06/15 Python
python实现列表中由数值查到索引的方法
2018/06/27 Python
Django实战之用户认证(用户登录与注销)
2018/07/16 Python
pycharm运行程序时在Python console窗口中运行的方法
2018/12/03 Python
Python3.5实现的罗马数字转换成整数功能示例
2019/02/25 Python
python循环定时中断执行某一段程序的实例
2019/06/29 Python
python多任务之协程的使用详解
2019/08/26 Python
python中delattr删除对象方法的代码分析
2020/12/15 Python
PHP数据运算类型都有哪些
2013/11/05 面试题
服装创业计划书范文
2014/02/05 职场文书
公司年会搞笑主持词
2014/03/24 职场文书
《厄运打不垮的信念》教学反思
2014/04/13 职场文书
开展批评与自我批评心得体会
2014/10/17 职场文书
工艺技术员岗位职责
2015/02/04 职场文书
埃及王子观后感
2015/06/16 职场文书
关于做家务的心得体会
2016/01/23 职场文书
《珍珠鸟》教学反思
2016/02/16 职场文书
Go timer如何调度
2021/06/09 Golang
nginx代理实现静态资源访问的示例代码
2022/07/07 Servers