基於jquery的滑鼠滾動放大縮小圖片

來源:互聯網
上載者:User

標籤:滑鼠滾動放大縮小圖片

    一直以來都想寫一個圖片放大和縮小的小玩意,本來以為會很複雜,這幾天自己思考了一下,原來是so easy啊。目前實現的放大縮小,主要時依據滑鼠的滾輪觸發事件來實現的,廢話少說直接上源碼。


<!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=utf-8" />

<title>圖片放大縮小</title>

<style type="text/css">

*{

margin:0; padding:0;

}

body { 

width:100%; height:100%; overflow:auto; 

}

#img {

display:block;

width:20%;

margin:20px auto;

cursor:-webkit-zoom-in;

cursor:-moz-zoom-in;

border:4px solid #ccc; 

}

</style>

<script src="./jquery/jquery-1.8.3.min.js"></script>   

<script type="text/javascript">

$(document).ready(function(){

var img=$("#img");

var i=0;

img.bind(‘DOMMouseScroll mousewheel function(e) {

 var ev=window.event||e;//處理瀏覽器安全色性問題

  var scrollUnit=Math.max(-1,Math.min(1,(ev.wheelDelta||-ev.detail)));

  var newWith=Math.max(100,Math.min(1600,parseInt(img.css("width"))+(30*scrollUnit)))+"px";

img.css("width",newWith);

console.log(i++);

console.log(img.css("width"));

  });

});

</script>

</head>


<body>

<img id="img" src="images/drag.jpg"/>

</body>

</html>

針對源碼,做幾點說明和補充:

1,ev.wheelDelta和ev.detail

    ev.wheelDelta, 除出Firefox外的瀏覽器IE,chrome,Opera、Safari基本支援屬性,該屬性取值為+,-,120;

     ev.detail,為Firefox特有的事件屬性,取值+,-3;

2,Math.min,Math.max

    這個地方的取最大值和最小值,最好自己琢磨一下,稍微有一個繞。

3,放大縮小的倍數和最值問題

   1和-1控制縮小和放大速率,絕對值越大縮放效果越明顯。

   100,1600,分別為縮小的最小值和放大的最大值。

基本就這麼多了,歡迎大家提出寶貴建議和想法。


本文出自 “7439523” 部落格,轉載請與作者聯絡!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.