jQuery實(shí)現(xiàn)滾動鼠標(biāo)放大縮小圖片的方法(附demo源碼下載)
本文實(shí)例講述了jQuery實(shí)現(xiàn)滾動鼠標(biāo)放大縮小圖片的方法。分享給大家供大家參考,具體如下:
在項(xiàng)目制作過程中,遇到了這么一個(gè)需求,就開發(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">滾動鼠標(biāo)中鍵,可以放大或者縮小圖片</span> </div>
在這個(gè)樣式中,我設(shè)置了圖片的樣式為670px,目的就是避免圖片過大的時(shí)候,顯示到了頁面外部的現(xiàn)象。
然后我使用了一個(gè)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 position var 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 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)滾動的時(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()可以保證在滾動圖片的過程中,頁面不會隨之滾動。
下面附上這個(gè)插件:
點(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ì)有所幫助。
- 基于jquery實(shí)現(xiàn)一張圖片點(diǎn)擊鼠標(biāo)放大再點(diǎn)縮小
- jquery實(shí)現(xiàn)頁面圖片等比例放大縮小功能
- 基于jquery的滾動鼠標(biāo)放大縮小圖片效果
- jQuery實(shí)現(xiàn)的點(diǎn)擊圖片居中放大縮小功能示例
- 利用jQuery插件imgAreaSelect實(shí)現(xiàn)圖片上傳裁剪(放大縮?。?/a>
- jQuery實(shí)現(xiàn)的圖片點(diǎn)擊放大縮小功能案例
- jQuery實(shí)現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁面布局
- 用JQuery模仿淘寶的圖片放大鏡顯示效果
- jquery圖片放大鏡功能的實(shí)例代碼
- jQuery+css實(shí)現(xiàn)的點(diǎn)擊圖片放大縮小預(yù)覽功能示例【圖片預(yù)覽 查看大圖】
相關(guān)文章
基于jQuery實(shí)現(xiàn)交互體驗(yàn)社會化分享代碼附源碼下載
基于jQuery實(shí)現(xiàn)交互體驗(yàn)社會化分享代碼附源碼下載。這是一款鼠標(biāo)點(diǎn)擊分享按鈕向右滑出騰訊微博,新浪微博,QQ空間,豆瓣,微信,二維碼分享等分享平臺,本段代碼比較實(shí)用,需要的朋友參考下吧2016-01-01基于jquery的點(diǎn)擊鏈接插入鏈接內(nèi)容的代碼
基于jquery的點(diǎn)擊鏈接插入鏈接內(nèi)容的代碼,感覺有bug,解決的朋友可以留言方便更多的朋友2012-07-07jquery.lazyload 實(shí)現(xiàn)圖片延遲加載jquery插件
看到了淘寶產(chǎn)品介紹中,圖片是在下拉滾動條時(shí)加載,這是一個(gè)很不錯(cuò)的用戶體驗(yàn)。減少了頁面加載的時(shí)間了,也減輕了服務(wù)器的壓力,就查了下用JQuery..2010-02-02Bootstrap框架建立樹形菜單(Tree)的實(shí)例代碼
這篇文章主要介紹了在Bootstrap框架下怎么去建立一個(gè)樹形菜單,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-10-10用JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法
本篇文章主要是對JQuery實(shí)現(xiàn)全選與取消的兩種簡單方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02jquery實(shí)現(xiàn)點(diǎn)擊文字可編輯并修改保存至數(shù)據(jù)庫
網(wǎng)上的方法只有點(diǎn)擊文字編輯并保持,但是沒有完整的代碼寫怎么保存到數(shù)據(jù)庫,本例用一條sql語句保存到數(shù)據(jù)庫2014-04-04Jquery進(jìn)度條插件 Progress Bar小問題解決
今天遇到一個(gè)進(jìn)度條的小問題,搞的我小糾結(jié)了一會,最后感謝同事分享文章,得以結(jié)局,呵呵,小經(jīng)驗(yàn)還是要保存的2011-07-07jQueryUI寫一個(gè)調(diào)整分類的拖放效果實(shí)現(xiàn)代碼
最近,想用jQuery做一個(gè)網(wǎng)頁的樹目錄結(jié)構(gòu),并且可以使用鼠標(biāo)拖動調(diào)整選項(xiàng)的位置。我在網(wǎng)上找了一下插件,基本上看了好幾款比較著名的,都覺得代碼太復(fù)雜了或者界面太丑了等各種不符合我的要求2012-05-05