欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jquery 图片缩放拖动的简单实例

程序员文章站 2022-08-10 14:22:39
一、不使用jquery,简单的缩放: . 代码如下:

一、不使用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>
<p align=center>
<span style="font-weight:bold">Size =
<span id="oCounter" style="color:red;">100%</span></span>
<img id="oImage" src="images/aaa.gif" onmousewheel="return Picture();" >
</p>
</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";
            }
            $("#pBlock").bind(event,
        function (e) {
            var evt = window.event || e;
            var newWidth;
            var newHeight;
            var newLeft;
            var newTop;
            var overHeight = $("#pBlock").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);
        }
        );

  nbsp;          $("#pBlock").bind("mousedown", function (e) {
                var xo = e.pageX;
                var yo = e.pageY;
                var imgLeft = $("#imgMain").position().left;
                var imgTop = $("#imgMain").position().top;
                var overHeight = $("#pBlock").height();
                $("#pBlock").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);
                });

            });

            $("#pBlock").bind("mouseup mouseout", function () {
                $("#pBlock").unbind("mousemove");
            });

        });
    </script>
</head>
<body>
    <p id="imgBox">
        <p style="width: 200px; height: 200px; cursor: pointer; position: relative; left: 0;
            top: 0; filter: alpha(opacity=0); opacity: 0; -moz-opacity: 0; background: blue;
            z-index: 999;" id="pBlock">
        </p>
        <img src="test.jpg" width="200" height="200" alt="" id="imgMain" />
    </p>
</body>
</html>