成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? jQuery實現滾動鼠標放大縮小圖片的方法(附demo源碼下載)
全站頭部文字 我要出現在這里
jQuery實現滾動鼠標放大縮小圖片的方法(附demo源碼下載) 642 2024-03-10   

本文實例講述了jQuery實現滾動鼠標放大縮小圖片的方法。分享給大家供大家參考,具體如下:

在項目制作過程中,遇到了這么一個需求,就開發了一個,記錄一下。

首先,需要定義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">滾動鼠標中鍵,可以放大或者縮小圖片</span>
</div>

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

然后我使用了一個jquery mousewheel 的插件來解決鼠標中鍵的滾動問題,下面是具體的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函數來獲取鼠標所處的位置,在IE9和firefox下面測試是可以使用的:

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

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

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;  // every step for scrolling down or up
$(this).removeAttr('style');

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

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;

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

下面附上這個插件:

點擊此處本站下載。

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery拖拽特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》

希望本文所述對大家jQuery程序設計有所幫助。



主站蜘蛛池模板: 沈敏| 无懈可击图片| 洪熙官个人资料简介| 雪中悍刀行第一季演员表| 华师大图书馆| 幻乐森林演员表| 影片 - theav| 狂野时代| 雾里看花电视剧剧情介绍| 《克莱默夫妇》 | 色在线视频播放| 生儿育女清宫表2024年清宫图| 托洛斯基| 台湾电视台| 阿尔法变频器说明书| 伊利亚| 文史茶馆| 男人不可以穷演员表| 黄海冰主演电视剧大全| 婚前婚后电影高清完整版| 电影不扣钮的女孩| 电视剧暗夜与黎明剧情介绍| a friend in need中文翻译| 汤浅政明| jenna haze| 美女上厕所| 次强音| 吻电影| 工业硫酸| 日韩电影免费观看高清完整版在线| 地板鞋编织方法的视频教程| 欧美变态sososo另类| 美女又黄又免费的视频| kaya| 天下第一楼剧情介绍| 蜘蛛夫人:超感觉醒 电影| 包法利夫人电影| 周翊然个人资料简介| 八角笼中电影| 张咏咏| 唐人街探案一免费观看完整版高清 |

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103