手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery 图片缩放拖动的简单实例
jquery 图片缩放拖动的简单实例
摘要:一、不使用jquery,简单的缩放:复制代码代码如下:onMouseWheelvarcount=10;functionPicture(){c...

一、不使用jquery,简单的缩放:

复制代码 代码如下:

<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<TITLE>onMouseWheel</TITLE>

<SCRIPT>

var count = 10;

function Picture()

{

count = Counting(count);

Resize(count);

return false;

}

function Counting(count){

if (event.wheelDelta >= 120)

count++;

else if (event.wheelDelta <= -120)

count--;

return count;

}

function Resize(count){

oImage.style.zoom = count + '0%';

oCounter.innerText = count + '0%';

}

</SCRIPT>

</HEAD>

<BODY>

<div align=center>

<span>Size =

<span id="oCounter">100%</span></span>

<img id="oImage" src="images/aaa.gif" onmousewheel="return Picture();" >

</div>

</BODY>

</HTML>

一、使用jquery,实现缩放和拖动:

复制代码 代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title></title>

<style type="text/css">

#imgBox

{

width: 200px;

height: 200px;

background: red;

overflow: hidden;

margin: auto;

position: relative;

}

#imgMain

{

position: relative;

top: -200px;

}

</style>

<script src="js/jquery-1.2.6.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">

$(function () {

var event;

if ($.browser.mozilla) {

event = "DOMMouseScroll";

}

else {

event = "mousewheel";

}

$("#divBlock").bind(event,

function (e) {

var evt = window.event || e;

var newWidth;

var newHeight;

var newLeft;

var newTop;

var overHeight = $("#divBlock").height();

if (evt.detail > 0 || evt.wheelDelta < 0) {

newWidth = $("#imgMain").width() - 20;

newHeight = $("#imgMain").height() - 20;

newLeft = $("#imgMain").position().left + 10;

newTop = $("#imgMain").position().top + 10 - overHeight;

}

else {

newWidth = $("#imgMain").width() + 20;

newHeight = $("#imgMain").height() + 20;

newLeft = $("#imgMain").position().left - 10;

newTop = $("#imgMain").position().top - 10 - overHeight;

}

$("#imgMain").css({ left: newLeft + "px", top: newTop + "px" });

$("#imgMain").width(newWidth);

$("#imgMain").height(newHeight);

}

);

$("#divBlock").bind("mousedown", function (e) {

var xo = e.pageX;

var yo = e.pageY;

var imgLeft = $("#imgMain").position().left;

var imgTop = $("#imgMain").position().top;

var overHeight = $("#divBlock").height();

$("#divBlock").bind("mousemove", function (e) {

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();

var x = e.pageX;

var y = e.pageY;

var bX = $("#imgBox").offset().left;

var bY = $("#imgBox").offset().top;

$("#imgMain").css("left", x - bX - (xo - bX) + imgLeft);

$("#imgMain").css("top", y - bY - (yo - bY) - overHeight + imgTop);

});

});

$("#divBlock").bind("mouseup mouseout", function () {

$("#divBlock").unbind("mousemove");

});

});

</script>

</head>

<body>

<div id="imgBox">

<div id="divBlock">

</div>

<img src="test.jpg" width="200" height="200" alt="" id="imgMain" />

</div>

</body>

</html>

【jquery 图片缩放拖动的简单实例】相关文章:

jQuery插件实现适用于移动端的地址选择器

jquery实现弹出层效果实例

JQuery分屏指示器图片轮换效果实例

jquery中map函数遍历数组用法实例

Jquery使用css方法改变样式实例

Jquery中基本选择器用法实例详解

jQuery插件制作之全局函数用法实例

javascript瀑布流式图片懒加载实例

jQuery处理图片加载失败的常用方法

js实现简单div拖拽功能实例

精品推荐
分类导航