在项目制作过程中,遇到了这么一个需求,就开发了一个,记录一下。
首先,需要定义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 1.2.2及其以上的版本。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2011-12-29 Asp.net配置文件中自定义节点详解