国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實(shí)現(xiàn)滾動鼠標(biāo)放大縮小圖片的方法(附demo源碼下載)

2019-11-20 10:26:56
字體:
供稿:網(wǎng)友

本文實(shí)例講述了jQuery實(shí)現(xiàn)滾動鼠標(biāo)放大縮小圖片的方法。分享給大家供大家參考,具體如下:

在項(xiàng)目制作過程中,遇到了這么一個需求,就開發(fā)了一個,記錄一下。

首先,需要定義html元素和css樣式:

<div style="position:relative;"><asp:Image ID="myImg" runat="server" Width="670px" /><span style="position:relative;display:none; background:wheat;border:1px solid gray;padding:3px;overflow:hidden;" id="NotificationMsg">滾動鼠標(biāo)中鍵,可以放大或者縮小圖片</span></div>

在這個樣式中,我設(shè)置了圖片的樣式為670px,目的就是避免圖片過大的時(shí)候,顯示到了頁面外部的現(xiàn)象。

然后我使用了一個jquery mousewheel 的插件來解決鼠標(biāo)中鍵的滾動問題,下面是具體的jquery操作代碼:

<script type="text/javascript">$(document).ready(function() {  var count = 0;  $("#ctl00_ContentPlaceHolder1_myImg").hover(function(e) {      var left = e.originalEvent.x || e.originalEvent.layerX || 0; //get the left position      var top = e.originalEvent.y || e.originalEvent.layerY || 0;  //get the top position      $("#NotificationMsg").css({ 'position': 'absolute', 'left': left, 'top': top });      $("#NotificationMsg").css("display", "block");  }, function() {    //alert('mouserout');    $("#NotificationMsg").css("display", "none");  }).mousewheel(function(event, delta, deltaX, deltaY) {    count++;    var height = $(this).attr("height");  //get initial height     var width = $(this).attr("width");   // get initial width    var stepex = height / width;  //get the percentange of height / width    var minHeight = 150;  // min height    var tempStep = 50;  // evey step for scroll down or up    $(this).removeAttr('style');    if (delta == 1) { //up      $(this).attr("height", height + count * tempStep);      $(this).attr("width", width + count * tempStep / stepex);    }    else if (delta == -1) { //down      if (height > minHeight)        $(this).attr("height", height - count * tempStep);      else        $(this).attr("height", tempStep);      if (width > minHeight / stepex)        $(this).attr("width", width - count * tempStep / stepex);      else        $(this).attr("width", tempStep / stepex);    }    event.preventDefault();    count = 0;  });});</script>

在這段代碼中,利用了originalEvent函數(shù)來獲取鼠標(biāo)所處的位置,在IE9和firefox下面測試是可以使用的:

var left = e.originalEvent.x || e.originalEvent.layerX || 0; //get the left positionvar top = e.originalEvent.y || e.originalEvent.layerY || 0;  //get the top position

然后在代碼中,我進(jìn)行了如下的操作來確定圖片的初始高度和寬度以及圖片顯示的寬高比(目的是實(shí)現(xiàn)等比例縮放):

var height = $(this).attr("height");  //get initial height var width = $(this).attr("width");   // get initial widthvar stepex = height / width;  //get the percentange of height / widthvar minHeight = 150;  // min heightvar tempStep = 50;  // every step for scrolling down or up$(this).removeAttr('style');

其中,tempStep主要是為了實(shí)現(xiàn)滾動的時(shí)候,能夠進(jìn)行縮小和放大的比率值。做了這之后,我移除了image的width樣式,主要是為了實(shí)現(xiàn)放大或者縮小。

if (delta == 1) { //up  $(this).attr("height", height + count * tempStep);  $(this).attr("width", width + count * tempStep / stepex);}else if (delta == -1) { //down  if (height > minHeight)    $(this).attr("height", height - count * tempStep);  else    $(this).attr("height", tempStep);  if (width > minHeight / stepex)    $(this).attr("width", width - count * tempStep / stepex);  else    $(this).attr("width", tempStep / stepex);}event.preventDefault();count = 0;

上面這段就比較簡單了,主要是進(jìn)行上下滾動判斷,然后等比例放大或者縮小圖片。event.preventDefault()可以保證在滾動圖片的過程中,頁面不會隨之滾動。

下面附上這個插件:

點(diǎn)擊此處本站下載。

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)

希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 疏附县| 武穴市| 商城县| 松潘县| 武冈市| 南溪县| 大埔县| 雅江县| 开封县| 扶沟县| 新田县| 贡觉县| 罗甸县| 宁强县| 栖霞市| 玉山县| 湘西| 博湖县| 通山县| 长宁区| 如皋市| 尼玛县| 本溪| 巴林左旗| 海晏县| 武隆县| 台北县| 冕宁县| 聂荣县| 垫江县| 毕节市| 那曲县| 醴陵市| 鄂伦春自治旗| 龙井市| 土默特左旗| 公主岭市| 泸水县| 那曲县| 阿拉善盟| 唐海县|