代码驿站移动版
频道导航
HTML/Xhtml
CSS
JavaScript
HTML5
PHP教程
ASP.NET
正则表达式
AJAX
ThinkPHP
Yii
MySQL
MariaDB
Oracle
MongoDB
Redis
DedeCMS
PHPCMS
帝国CMS
WordPress
Discuz
其它CMS
Zend Studio
Sublime
Notepad
Dreamweaver
Windows
Linux
Nginx
Apache
IIS
CentOS
Ubuntu
Debian
网站优化
工具资源
PHP源码
ASP.NET源码
其它源码
图标素材
按钮素材
字体素材
DedeCMS模板
帝国CMS模板
PHPCMS模板
WordPress模板
Discuz!模板
单页模板
开发软件下载
服务器软件下载
广告投放
联系我们
版权申明
软件编程
网页前端
移动开发
数据库
服务器
脚本语言
PHP代码
JAVA代码
Python代码
Android代码
当前位置:
主页
>
网页前端
>
JavaScript代码
>
js 图片缩放特效代码
时间:2021-06-09 08:05:19 | 栏目:
JavaScript代码
| 点击:次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>图片特效</title> <style type="text/css"> .lightbox{width:300px;background:#FFFFFF;border:1px solid #ccc;line-height:25px; top:5%; left:5%;} .lightbox dt{background:#f4f4f4;} </style> <script type="text/javascript"> var isIE = (document.all) ? true : false; var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6); var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }; var Extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } }; var Bind = function(object, fun) { return function() { return fun.apply(object, arguments); } }; var Each = function(list, fun) { for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); } }; var Contains = function(a, b) { return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16); }; var OverLay = Class.create(); OverLay.prototype = { initialize: function(options) { this.SetOptions(options); this.Lay = $(this.options.Lay) || document.body.insertBefore(document.createElement("div"), document.body.childNodes[0]); this.Color = this.options.Color; this.Opacity = parseInt(this.options.Opacity); this.zIndex = parseInt(this.options.zIndex); with(this.Lay.style){ display = "none"; zIndex = this.zIndex; left = top = 0; position = "fixed"; width = height = "100%"; } if(isIE6){ this.Lay.style.position = "absolute"; //ie6设置覆盖层大小程序 this._resize = Bind(this, function(){ this.Lay.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px"; this.Lay.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px"; }); //遮盖select this.Lay.innerHTML = '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>' } }, //设置默认属性 SetOptions: function(options) { this.options = {//默认值 Lay: null,//覆盖层对象 Color: "#000",//背景色 Opacity: 50,//透明度(0-100) zIndex: 1000//层叠顺序 }; Extend(this.options, options || {}); }, //显示 Show: function() { //兼容ie6 if(isIE6){ this._resize(); window.attachEvent("onresize", this._resize); } //设置样式 with(this.Lay.style) { //设置透明度 isIE ? filter = "alpha(opacity:" + this.Opacity + ")" : opacity = this.Opacity / 100; backgroundColor = this.Color; display = "block"; } }, //关闭 Close: function() { this.Lay.style.display = "none"; if(isIE6){ window.detachEvent("onresize", this._resize); } } }; var LightBox = Class.create(); LightBox.prototype = { initialize: function(box, options) { this.Box = $(box);//显示层 this.OverLay = new OverLay(options);//覆盖层 this.SetOptions(options); this.Fixed = !!this.options.Fixed; this.Over = !!this.options.Over; this.Center = !!this.options.Center; this.onShow = this.options.onShow; this.Box.style.zIndex = this.OverLay.zIndex + 1; this.Box.style.display = "none"; //兼容ie6用的属性 if(isIE6) { this._top = this._left = 0; this._select = []; this._fixed = Bind(this, function(){ this.Center ? this.SetCenter() : this.SetFixed(); }); } }, //设置默认属性 SetOptions: function(options) { this.options = {//默认值 Over: true,//是否显示覆盖层 Fixed: false,//是否固定定位 Center: false,//是否居中 onShow: function(){}//显示时执行 }; Extend(this.options, options || {}); }, //兼容ie6的固定定位程序 SetFixed: function() { this.Box.style.top = document.documentElement.scrollTop - this._top + this.Box.offsetTop + "px"; this.Box.style.left = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft + "px"; this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft; }, //兼容ie6的居中定位程序 SetCenter: function() { this.Box.style.marginTop = document.documentElement.scrollTop - this.Box.offsetHeight / 2 + "px"; this.Box.style.marginLeft = document.documentElement.scrollLeft - this.Box.offsetWidth / 2 + "px"; }, //显示 Show: function(options) { //固定定位 this.Box.style.position = this.Fixed && !isIE6 ? "fixed" : "absolute"; //覆盖层 this.Over && this.OverLay.Show(); this.Box.style.display = "block"; //居中 if(this.Center) { this.Box.style.top = this.Box.style.left = "50%"; //设置margin if(this.Fixed) { this.Box.style.marginTop = - this.Box.offsetHeight / 2 + "px"; this.Box.style.marginLeft = - this.Box.offsetWidth / 2 + "px"; }else{ this.SetCenter(); } } //兼容ie6 if(isIE6) { if(!this.Over) { //没有覆盖层ie6需要把不在Box上的select隐藏 this._select.length = 0; Each(document.getElementsByTagName("select"), Bind(this, function(o){ if(!Contains(this.Box, o)){ o.style.visibility = "hidden"; this._select.push(o); } })) } //设置显示位置 this.Center ? this.SetCenter() : this.Fixed && this.SetFixed(); //设置定位 this.Fixed && window.attachEvent("onscroll", this._fixed); } this.onShow(); }, //关闭 Close: function() { this.Box.style.display = "none"; this.OverLay.Close(); if(isIE6) { window.detachEvent("onscroll", this._fixed); Each(this._select, function(o){ o.style.visibility = "visible"; }); } } }; function ZoomImg(o) { var zoom = parseInt(o.style.zoom, 10) || 100; zoom += event.wheelDelta / 12; if(zoom >60&&zoom<120) o.style.zoom = zoom + '%'; return false; } function ResizeImage() { var img=document.getElementById("imgclose"); var a=img.width; var b=img.height; //移除img的width,这样就得到原始的长了,不过图片也放大了,上面的代码不会,只是使用Image对象来获取原始的长 img.removeAttribute("width"); img.removeAttribute("height"); var w=img.width; var h=img.height; document.getElementById("").style.width=w; document.getElementById("").style.height=h; alert(w+" "+h); } </script> </head> <body> <table id="idBox" class="lightbox"> <tr id="idBoxHead" style="text-align:right"> <td> <span style="font-size:12px">鼠标滚轮放大图片</span> <a href="2.jpg" target="_blank"><img src="newwindow.gif" onclick="" alt="" width="16" height="16" border="0"/></a> <img src="resize.gif" onclick="ResizeImage()" alt="" width="16" height="16" /> <img src="close.gif" id="idBoxCancel" alt="close" width="11" height="11"/> </td> </tr> <tr style="text-align:left"> <td><img src="2.jpg" height="500" onmousewheel="return ZoomImg(this)" style="width:800px" id="imgclose" alt="" /></td> </tr> </table> <div> <img src="2.jpg" alt="" id="iimg" width="120" height="100"/> </div> <script language="javascript" type="text/javascript"> var box = new LightBox("idBox"); $("idBoxCancel").onclick = function(){ box.Close(); } $("imgclose").onclick = function(){ box.Close(); } $("iimg").onclick = function(){ box.Show(); } </script> </body> </html>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
Javascript的匿名函数小结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
兼容浏览器的js事件绑定函数(详解)
相关文章
10-19
javascript上下左右定时滚动插件
11-23
Webpack中loader打包各种文件的方法实例
11-27
Javascript实例教程(19) 使用HoTMetal(2)
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长