javascript 如何实现图片的上下滑动并加入缩放效果同时限制在容器内?
在现代网页设计中,常常需要对图片进行交互性操作和效果增强。其中,图片的上下滑动和缩放效果是常见的需求。本文将介绍如何使用javascript实现这些效果,并且限制在容器内。
一、上下滑动效果的实现
实现图片的上下滑动效果主要依赖于鼠标或触摸事件,并且需要控制图片的位置。
首先,在html部分,我们创建一个容器和一个图片元素:
<div id="container"> <img id="image" src="img.jpg"></div>
然后,在javascript中,我们需要为图片元素添加事件监听器,并且根据鼠标或触摸事件的位置变化,来移动图片的位置:
var container = document.getelementbyid('container');var image = document.getelementbyid('image');var starty; // 记录初始位置image.onmousedown = start;image.addeventlistener('touchstart', start, false);function start(e) { e.preventdefault(); if (e.touches) { starty = e.touches[0].clienty; document.addeventlistener('touchmove', move, false); document.addeventlistener('touchend', end, false); } else { starty = e.clienty; document.onmousemove = move; document.onmouseup = end; }}function move(e) { var currenty = e.touches ? e.touches[0].clienty : e.clienty; var diffy = currenty - starty; image.style.top = image.offsettop + diffy + 'px';}function end() { document.removeeventlistener('touchmove', move, false); document.removeeventlistener('touchend', end, false); document.onmousemove = null; document.onmouseup = null;}
通过以上代码,当用户按下鼠标或触摸屏幕时,会记录下初始的位置,并且随着鼠标或手指的移动,图片的位置也会随之改变。在结束操作时,移除事件监听器。
二、缩放效果的实现
实现图片的缩放效果是基于鼠标或触摸事件的位置和手势的判断。下面是一个例子,使用手势判断缩放:
var scalingfactor = 1.0; // 初始化缩放比例var gesturestartdistance; // 记录初始手势距离image.addeventlistener('gesturestart', start, false);image.addeventlistener('gesturechange', change, false);image.addeventlistener('gestureend', end, false);function start(e) { e.preventdefault(); gesturestartdistance = getgesturedistance(e);}function change(e) { var currentdistance = getgesturedistance(e); scalingfactor = currentdistance / gesturestartdistance; image.style.transform = 'scale(' + scalingfactor + ')';}function end() { gesturestartdistance = null;}function getgesturedistance(e) { var x1 = e.touches[0].pagex; var y1 = e.touches[0].pagey; var x2 = e.touches[1].pagex; var y2 = e.touches[1].pagey; return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2));}
在以上代码中,当手指开始接触屏幕时,记录下初始手势距离。当手指移动时,通过计算当前手势距离与初始手势距离的比例,来设置缩放比例,并应用到图片上。
三、限制在容器内
为了保证图片在容器内部滑动和缩放,并且不溢出容器范围,我们需要进行一些位置和尺寸的判断。
首先,在css部分,设置容器的宽度和高度,并给容器添加样式overflow: hidden;来隐藏溢出内容:
#container { width: 500px; height: 500px; overflow: hidden;}
然后,在javascript中,我们需要在滑动和缩放的过程中,判断图片的位置和尺寸是否超出容器的边界,并进行调整:
function move(e) { var currenty = e.touches ? e.touches[0].clienty : e.clienty; var diffy = currenty - starty; var mintop = container.clientheight - image.height; // 图片最小可到达的top值 var maxtop = 0; // 图片最大可到达的top值 var newtop = image.offsettop + diffy; newtop = math.max(mintop, math.min(maxtop, newtop)); image.style.top = newtop + 'px';}function change(e) { var currentdistance = getgesturedistance(e); scalingfactor = currentdistance / gesturestartdistance; var newwidth = image.width * scalingfactor; var newheight = image.height * scalingfactor; var minwidth = container.clientwidth; var minheight = container.clientheight; var maxwidth = image.width; var maxheight = image.height; newwidth = math.max(minwidth, math.min(maxwidth, newwidth)); newheight = math.max(minheight, math.min(maxheight, newheight)); image.style.width = newwidth + 'px'; image.style.height = newheight + 'px';}
通过以上代码,我们会根据容器的尺寸和图片的尺寸,计算出最小和最大的top值和尺寸,并且在滑动和缩放的过程中,进行判断和调整。
综上所述,通过javascript实现了图片的上下滑动并加入缩放效果,并且限制在容器内。通过鼠标或触摸事件的监听和位置的计算,实现了滑动效果。通过手势事件的监听和距离的计算,实现了缩放效果。通过对位置和尺寸的判断,实现了限制在容器内的效果。
以上就是javascript 如何实现图片的上下滑动并加入缩放效果同时限制在容器内?的详细内容。