jQuery實(shí)現(xiàn)滾動(dòng)鼠標(biāo)放大縮小圖片的方法(附demo源碼下載)
來(lái)源:易賢網(wǎng) 閱讀:1630 次 日期:2016-08-02 15:19:59
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery實(shí)現(xiàn)滾動(dòng)鼠標(biāo)放大縮小圖片的方法(附demo源碼下載)”,方便廣大網(wǎng)友查閱!

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

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

首先,需要定義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">滾動(dòng)鼠標(biāo)中鍵,可以放大或者縮小圖片</span>

</div>

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

然后我使用了一個(gè)jquery mousewheel 的插件來(lái)解決鼠標(biāo)中鍵的滾動(dòng)問(wèn)題,下面是具體的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ù)來(lái)獲取鼠標(biāo)所處的位置,在IE9和firefox下面測(cè)試是可以使用的:

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

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

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主要是為了實(shí)現(xiàn)滾動(dòng)的時(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;

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

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

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)