javascript放大镜代码

JavaScript028

javascript放大镜代码,第1张

同命相连,我也在找这个效果,失望啊!

发现有教程,可是运行不起来,看你会吗?

picclass.asp 图片处理类文件

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<%

'//////////// GPS:Get Picture Size //////////////

'//////////////利用ADODB.stream获取图片尺寸//////////////

'/////////Cited By Leon(心晴) 2005年8月11日//////////

Class GPS

Dim aso

Private Sub Class_Initialize

Set aso=CreateObject("Adodb.Stream")

aso.Mode=3

aso.Type=1

aso.Open

End Sub

Private Sub Class_Terminate

set aso=nothing

End Sub

Private Function Bin2Str(Bin)

Dim I, Str

For I=1 to LenB(Bin)

clow=MidB(Bin,I,1)

if AscB(clow)<128 then

Str = Str &Chr(ASCB(clow))

Else

I=I+1

if I <= LenB(Bin) then Str = Str &Chr(ASCW(MidB(Bin,I,1)&clow))

end If

Next

Bin2Str = Str

End Function

Private Function Num2Str(num,base,lens)

'GPS (2005-8-11)

dim ret

ret = ""

while(num>=base)

ret = (num mod base) &ret

num = (num - num mod base)/base

wend

Num2Str = right(string(lens,"0") &num &ret,lens)

End Function

Private Function Str2Num(str,base)

'GPS (2005-8-11)

dim ret

ret = 0

for i=1 to len(str)

ret = ret *base + cint(mid(str,i,1))

next

Str2Num=ret

End Function

Private Function BinVal(bin)

'GPS (2002-8-11)

dim ret

ret = 0

for i = lenb(bin) to 1 step -1

ret = ret *256 + ascb(midb(bin,i,1))

next

BinVal=ret

End Function

Private Function BinVal2(bin)

'GPS (2002-8-11)

dim ret

ret = 0

for i = 1 to lenb(bin)

ret = ret *256 + ascb(midb(bin,i,1))

next

BinVal2=ret

End Function

'///以下是调用代码///

Function getImageSize(filespec)

'GPS (2002-8-11)

dim ret(3)

aso.LoadFromFile(filespec)

bFlag=aso.read(3)

select case hex(binVal(bFlag))

case "4E5089":

aso.read(15)

ret(0)="PNG"

ret(1)=BinVal2(aso.read(2))

aso.read(2)

ret(2)=BinVal2(aso.read(2))

case "464947":

aso.read(3)

ret(0)="GIF"

ret(1)=BinVal(aso.read(2))

ret(2)=BinVal(aso.read(2))

case "535746":

aso.read(5)

binData=aso.Read(1)

sConv=Num2Str(ascb(binData),2 ,8)

nBits=Str2Num(left(sConv,5),2)

sConv=mid(sConv,6)

while(len(sConv)<nBits*4)

binData=aso.Read(1)

sConv=sConv&Num2Str(ascb(binData),2 ,8)

wend

ret(0)="SWF"

ret(1)=int(abs(Str2Num(mid(sConv,1*nBits+1,nBits),2)-Str2Num(mid(sConv,0*nBits+1,nBits),2))/20)

ret(2)=int(abs(Str2Num(mid(sConv,3*nBits+1,nBits),2)-Str2Num(mid(sConv,2*nBits+1,nBits),2))/20)

case "FFD8FF":

do

do: p1=binVal(aso.Read(1)): loop while p1=255 and not aso.EOS

if p1>191 and p1<196 then exit do else aso.read(binval2(aso.Read(2))-2)

do:p1=binVal(aso.Read(1)):loop while p1<255 and not aso.EOS

loop while true

aso.Read(3)

ret(0)="JPG"

ret(2)=binval2(aso.Read(2))

ret(1)=binval2(aso.Read(2))

case else:

if left(Bin2Str(bFlag),2)="BM" then

aso.Read(15)

ret(0)="BMP"

ret(1)=binval(aso.Read(4))

ret(2)=binval(aso.Read(4))

else

ret(0)=""

end if

end select

ret(3)="width=""" &ret(1) &""" height=""" &ret(2) &""""

getimagesize=ret

End Function

End Class

%>

图片调用文件----------------------------------------------------------------------------------------------------------

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<!--#include file="picclass.asp"-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>

<SCRIPT LANGUAGE="JAVASCRIPT">

<!--

function maxview(){

if (window.screen){

var w=screen.availwidth

var h=screen.availheight

window.moveTo(0,0)

window.resizeTo(w,h)

}

}

// -->

</SCRIPT>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title></title>

<SCRIPT src="zoom.js" type=text/javascript></SCRIPT>

</head>

<body onLoad="maxview()OnLoadMainFunction()">

<table width="905" border="0" align="center" cellpadding="15" cellspacing="1">

<tr>

<td valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="15"

cellpadding="0">

<tr>

<td valign="top">

<%

if request("showid")<>"" then

sql="update lei_product set dianjishu=dianjishu+1 where id="&(request("showid"))

conn.execute(sql)

set rs=server.CreateObject("adodb.recordset")

showstr="select * from lei_product where id="&(request("showid"))

rs.open showstr,conn,1,1

if not rs.bof and not rs.eof then

'/////获取ShowImg.asp的绝对路径/////

Dim curFile

curFile=Server.mappath(Request.servervariables("PATH_INFO"))

Dim curfilename,filename

'/////图片相对路径(存于数据库中)

'cufilename=rs("ImgURL")

cufilename="../admin/upimg/"&rs("bigimg2")

cufilename2="../admin/upimg/"&rs("bigimg")

'/////因为ShowImg.asp与images在同一目录,所以我们用instrrev获取images的路径/////

filename=left(curFile,instrrev(curFile,"\"))&cufilename

filename2=left(curFile,instrrev(curFile,"\"))&cufilename2

'/////建立GPS类实体/////

Dim GetPicSize

Set GetPicSize=new GPS

Set fs=Server.CreateObject("Scripting.FileSystemObject")

'/////获取图片类型/////

Dim PicSuffixName

Dim PicSuffixName2

if (fs.FileExists(filename)=false) then

Response.Write("<script>alert('文件加载失败,请确认文件是否存在')history.back(-1)</script>")

Response.End()

end if

if (fs.FileExists(filename2)=false) then

Response.Write("<script>alert('文件加载失败,请确认文件是否存在')history.back(-1)</script>")

Response.End()

end if

PicSuffixName=fs.GetExtensionName(filename)

PicSuffixName2=fs.GetExtensionName(filename2)

Dim PD '//Picture Dimension

Dim PD2 '//Picture Dimension

Dim PWidth,PHeight

Dim PWidth2,PHeight2

PD=GetPicSize.GetImageSize(filename)

PD2=GetPicSize.GetImageSize(filename2)

PWidth=PD(1) '//获取图片宽度

PWidth2=PD2(1) '//获取图片宽度

PHeight=PD(2) '//获取图片高度

PHeight2=PD2(2) '//获取图片高度

Set fs=Nothing

Set GetPicSize=Nothing

%>

<SCRIPT type=text/javascript>

var usezoom = 1

var itemimagedir = ''

var zoom_bigimagediv = 'bigimagediv'

var zoom_bigimagetablediv = 'bigimagetablediv'

var zoom_highlightdiv = 'highlightdiv'

var zoom_bigimageouterdiv = 'bigimageouterdiv'

var zoom_viewportwid = 400//大图图片显示区域

var zoom_viewporthei = 320//大图片显示区域

var zoom_mainimagewid = <%=PWidth2%>//小图宽

var zoom_mainimagehei = <%=PHeight2%>//小图高

var zoom_mainimageurl = '../admin/upimg/<%=rs("bigimg2")%>'

var zoom_zoomimagewid = <%=PWidth%>//原图大小宽

var zoom_zoomimagehei = <%=PHeight%>//原图大小高

var zoom_zoomimageurl = '../admin/upimg/<%=rs("bigimg2")%>'//大图地址

var zoom_bigimagetablexoffset = 1

var zoom_bigimagetableyoffset = 21

var zoom_displayxposition = 0

var zoom_displayyposition = 0

var zoom_miniimagex = 0

var zoom_miniimagey = 0

var zoom_highlightbgcolor = 'ffffff'

var zoom_highlightimage = 'highlight.gif'

function GetZoomObjectData() {

var position

if (!InIE &&!InNS6 &&!DOMCompatible)

return

position = MeMSOObjectGetPosition('mainimage')

zoom_miniimagex = position.x

zoom_miniimagey = position.y

position = MeMSOObjectGetPosition('locatezoomim')

zoom_displayxposition = position.x//显示图片的位置

zoom_displayyposition = position.y - 21

Zoom_StartupZoomRegion()

}

onloadlist += 'GetZoomObjectData()'

</SCRIPT>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">

<tr>

<td bgcolor="#FFFFFF"><div align="center">

<table cellspacing="0" cellpadding="0" width="100%" border="0">

<tbody>

<tr>

<td valign="center" align="middle" rowspan="4"><table cellspacing="1"

cellpadding="2" width="<%=PWidth2%>"

bgcolor="#efe6dd" border="0" height="<%=PHeight2%>">

<tbody>

<tr>

<td bgcolor="#ffffff"><img

src="..\admin\upimg\<%=rs("bigimg")%>" name="mainimage"

border="0" width="<%=PWidth2%>" height="<%=PHeight2%>"

id="mainimage" /></td>

</tr>

</tbody>

</table>

<br />

<a href="#" target="_blank">点击看大图</a></td>

<td valign="bottom" align="left" height="5"><img height="5"

src="dot_line.gif" width="401" /></td>

</tr>

<tr>

<td valign="bottom" align="left" width="420" height="1"><table cellspacing="0"

cellpadding="0" width="100%"

border="0">

<tbody>

<tr>

<td colspan="2"><img id="locatezoomim" height="1"

src="dot_1.gif" width="1"

name="locatezoomim" /></td>

</tr>

</tbody>

</table></td>

</tr>

<tr>

<td valign="top" align="left" width="425"

background="bigImg.gif"

height="330"><p>  </p></td>

</tr>

<tr>

<td align="middle"><div id="highlightdiv"

style="Z-INDEX: 2LEFT: -2000pxPOSITION: absoluteTOP: -2000px

visibility: hidden"></div>

<div id="bigimageouterdiv"

style="Z-INDEX: 4LEFT: -2000pxVISIBILITY: visibleOVERFLOW:

hiddenWIDTH: 300pxPOSITION: absoluteTOP: -2000pxHEIGHT: 300px">

<div id="bigimagediv"

style="Z-INDEX: 4LEFT: -2000pxPOSITION: absoluteTOP: -2000px

visibility: hidden"><img

id="mainzoomimage" height="2400" src="" width="800" border="0"

name="mainzoomimage" /></div>

</div>

<div id="bigimagetablediv" style="Z-INDEX: 3LEFT: -2000pxPOSITION:

absoluteTOP: -2000pxvisibility: hidden"></div></td>

</tr>

</tbody>

</table>

</div>

<div align="right"></div></td>

</tr>

</table>

<%end if

rs.close

set rs = nothing

%>

<%end if

%>

</td>

</tr>

</table></td>

</tr>

</table>

<table width="905" height="8" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td></td>

</tr>

</table>

</body>

</html>

(function($){//定义作用域

$.fn.imagezoom=function(options){/*自定义插件imageszoom,options代表形参(属性自定义)*/

var settings={

xzoom: 350, /*放大图的宽度(默认是 350)*/

yzoom: 350, /*放大图的高度(默认是 350)*/

offset: 10, /*离原图的距离(默认是 10)*/

position: "right", /*放大图的定位(默认是 "right")*/

preload:1

}

if(options){

$.extend(settings,options)

}

var noalt=''

var self=this

$(this).bind("mouseenter",function(ev){/*鼠标经过时添加一个事件处理程序*/

var imageLeft=$(this).offset().left

var imageTop=$(this).offset().top

var imageWidth=$(this).get(0).offsetWidth

var imageHeight=$(this).get(0).offsetHeight

var boxLeft=$(this).parent().offset().left

var boxTop=$(this).parent().offset().top

var boxWidth=$(this).parent().width()

var boxHeight=$(this).parent().height()

noalt=$(this).attr("alt")

var bigimage=$(this).attr("rel")

$(this).attr("alt",'')

if($("div.zoomDiv").get().length==0){

$(document.body).append("<div class='zoomDiv'><img class='bigimg' src='"+bigimage+"'/></div>"+

"<div class='zoomMask'></div>")

}

if(settings.position=="right"){

if(boxLeft+boxWidth+settings.offset+settings.xzoom>screen.width){

leftpos=boxLeft-settings.offset-settings.xzoom

}else{

leftpos=boxLeft+boxWidth+settings.offset

}

}else{

leftpos=imageLeft-settings.xzoom-settings.offset

if(leftpos<0){

leftpos=imageLeft+imageWidth+settings.offset

}

}

$("div.zoomDiv").css({top:boxTop,left:leftpos})

$("div.zoomDiv").width(settings.xzoom)

$("div.zoomDiv").height(settings.yzoom)

$("div.zoomDiv").show()

$(this).css('cursor','crosshair')/*光标呈现十字线*/

$(document.body).mousemove(function(e){/*当移动鼠标时*/

mouse=new MouseEvent(e)

if(mouse.x<imageLeft||mouse.x>imageLeft+imageWidth||mouse.y<imageTop||mouse.y>imageTop+imageHeight){

mouseOutImage()/*判断鼠标是否超出图片范围*/

return

}

var bigwidth=$(".bigimg").get(0).offsetWidth/*最大宽度*/

var bigheight=$(".bigimg").get(0).offsetHeight/*最大高度*/

var scaley='x'/*x轴比例 */

var scalex='y'/*y轴比例 */

/*随鼠标移动显示大图*/

if(isNaN(scalex)|isNaN(scaley)){/*x、y轴比例不是数字时*/

var scalex=(bigwidth/imageWidth)

var scaley=(bigheight/imageHeight)

$("div.zoomMask").width((settings.xzoom)/scalex)

$("div.zoomMask").height((settings.yzoom)/scaley)

$("div.zoomMask").css('visibility','visible')/*规定元素可见*/

}

xpos=mouse.x-$("div.zoomMask").width()/2

ypos=mouse.y-$("div.zoomMask").height()/2

xposs=mouse.x-$("div.zoomMask").width()/2-imageLeft

yposs=mouse.y-$("div.zoomMask").height()/2-imageTop

xpos=(mouse.x-$("div.zoomMask").width()/2<imageLeft)

? imageLeft:(mouse.x+$(".zoomMask").width()/2>imageWidth+imageLeft)

? (imageWidth+imageLeft-$("div.zoomMask").width()):xpos

ypos=(mouse.y-$("div.zoomMask").height()/2<imageTop)

? imageTop:(mouse.y+$("div.zoomMask").height()/2>imageHeight+imageTop)

? (imageHeight+imageTop-$("div.zoomMask").height()):ypos

$("div.zoomMask").css({top:ypos,left:xpos})

$("div.zoomDiv").get(0).scrollLeft=xposs*scalex

$("div.zoomDiv").get(0).scrollTop=yposs*scaley

})

})

function mouseOutImage(){/*定义鼠标离开图片方法*/

$(self).attr("alt",noalt)

$(document.body).unbind("mousemove")/*移除在页面中鼠标指针事件*/

$("div.zoomMask").remove()/*移除所有的div.zoomMask*/

$("div.zoomDiv").remove()/*移除所有的div.zoomDiv*/

}

count=0

if(settings.preload){

/*在boby元素的结尾(仍然在内部)插入指定内容*/

$('body').append("<div style='display:none' class='jqPreload"+count+"'></div>")

$(this).each(function(){/*规定为每个匹配元素规定运行的函数*/

var imagetopreload=$(this).attr("rel")/*图片预加载*/

var content=jQuery('.jqPreload'+count+'').html()

jQuery('.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">')

})

}

}

})(jQuery)

function MouseEvent(e){/*记录鼠标x,y坐标*/

this.x=e.pageX/*鼠标指针位置*/

this.y=e.pageY

}

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>放大镜</title>

<style>

img{ vertical-align:bottom}

.mod_zoom{ overflow:hiddenzoom:1}

.mod_zoom .p1 { float:leftposition:relativeheight:350pxwidth:350pxmargin-right:5px}

.mod_zoom .p1 .mask{ position:absoluteleft:0top:0width:175pxheight:175pxbackground-color:#fede4fopacity:0.3filter: alpha(opacity=30)display:none}

.mod_zoom .p2{ position:relativewidth:400pxheight:400pxoverflow:hiddendisplay:none}

.mod_zoom .p2 .img{ position:absoluteleft:0top:0}

.mod_zoom .ph{width:100%height:350pxposition:absolutetop:0left:0cursor:crosshair

/*background-color:red*/

/*如果给它绑定事件处理函数,IE中不设置background-color属性就不触发事件*/ }

</style>

</head>

<body>

<div>

<div class="mod_zoom">

<div class="p1" id="p1">

<img src="http://img14.360buyimg.com/n1/4071/b350e77e-fc74-4173-81b5-dfe54f425ef6.jpg" id="z1" />

<span class="mask" id="m"></span>

<span class="ph" id="eventproxy"></span>

</div>

<div class="p2" id="p2">

<img src="http://img14.360buyimg.com/n0/4071/b350e77e-fc74-4173-81b5-dfe54f425ef6.jpg" class="img" id="z2" />

</div>

</div>

</div>

<script>

function PhotoZoomer(elements){

this.mask = elements.mask//蒙版

this.container = elements.container //原图容器

this.originimg = elements.originimg//原图

this.eventproxy = elements.eventproxy

this.bigContainer = elements.bigContainer//大图容器

this.bigimg = elements.bigimg//大图

this.visible = false

this._bind()

}

PhotoZoomer.prototype = {

display: function(style){

var self = this

self.mask.style.display = style

self.bigContainer.style.display = style

},

//计算放大蒙版位置

zoom: function(clientX, clientY){

var self = this,

//位置比例

rate = {},

//放大蒙版最大活动范围

maxrange = {

offsetLeft: self.container.offsetWidth - self.mask.offsetWidth,

offsetTop: self.container.offsetHeight - self.mask.offsetHeight

},

//mask left

left = clientX - self.container.offsetLeft - self.mask.offsetWidth/2,

//mask top

top = clientY - self.container.offsetTop - self.mask.offsetHeight/2

if(left <0) {

left = 0

}else if(left>maxrange.offsetLeft) {

left = maxrange.offsetLeft

}

if(top <0) {

top = 0

}else if(top >maxrange.offsetTop){

top = maxrange.offsetTop

}

//alert(maxrange.offsetTop)

rate.left = left / maxrange.offsetLeft

rate.top = top / maxrange.offsetTop

self.mask.style.left = left + 'px'

self.mask.style.top = top + 'px'

self.bigimg.style.left = -rate.left * (self.bigimg.offsetWidth - self.bigContainer.offsetWidth) + "px"

self.bigimg.style.top = -rate.top * (self.bigimg.offsetHeight - self.bigContainer.offsetHeight) + "px"

},

_bind: function(){

var self = this

self.container.onmouseover = function(e){

e = e || window.event

var target = e.targe || e.srcElement

self.display("block")

this.visible = true

}

self.container.onmouseout = function(e){

e = e || window.event

var target = e.targe || e.srcElement

self.display("none")

this.visible = false

}

self.container.onmousemove = function(e){

e = e || window.event

if(!this.visible )return//防止元素大小计算错误

self.zoom(e.clientX, e.clientY)

}

}

}

function get(id){

return document.getElementById(id)

}

var elements = {

mask: get("m"),

container: get("p1"),

originimg: get("z1"),

bigContainer: get("p2"),

bigimg: get("z2"),

eventproxy: get("eventproxy")

}

var zoomer = new PhotoZoomer(elements)

// alert(elements.container.offsetParent.tagName)

</script>

</body>

</html>