鼠标在屏幕左侧移动图像向左移动,鼠标在屏幕右侧移动时相同
我正在尝试获取大约 1920x1200px 的图像,以在 800x600px 的浏览器窗口上平移。 因此,如果我的鼠标位于浏览器窗口的左上角,则图像会对齐,因此它的左上角边距位于浏览器窗口的左上角。右下角也是如此。
因此,如果鼠标位于屏幕中央,则图像也应该居中。
我不确定需要什么计算,因为我的数学有点生疏。
目前,我正在使用一些 javascript,它只是使用 CSS 的上/左属性移动图像,但没有取得太大成功,因为它只是从上/左角移动图片。
我还在 css 中将图像的位置设置为绝对位置。
function updateImgPosition( e )
{
var avatar = document.getElementById("avatar");
// Width
var windowWidth = window.innerWidth;
var mouseWidthLocation = e.x;
var percentOfWidth = (100 / windowWidth) * mouseWidthLocation;
// Height
var windowHeight = window.innerHeight;
var mouseHeightLocation = e.y;
var percentOfHeight = (100 / windowHeight) * mouseHeightLocation;
avatar.style.top = percentOfHeight + "%";
avatar.style.left = percentOfWidth + "%";
}
document.onmousemove = updateImgPosition;
这是我所拥有的演示: http://jsfiddle.net/uQAmQ/1/
I'm trying to get an image that is around 1920x1200px to pan around on a 800x600px browser window.
So if my mouse is on the top-left of the browser window the image is alined so it's top-left margins are on the top-left of the browser window. The same goes for the bottum-right.
So if the mouse is in the centre of the screen the image should be centered too.
Im not sure what calculations are needed as my math is a bit rusty.
Currently I'm using a bit of javascript that just moves the image using CSS's top/left properties but without much success as it's just moving the picture around from it's top/left corner.
I'v also set the image's position to absolute in css.
function updateImgPosition( e )
{
var avatar = document.getElementById("avatar");
// Width
var windowWidth = window.innerWidth;
var mouseWidthLocation = e.x;
var percentOfWidth = (100 / windowWidth) * mouseWidthLocation;
// Height
var windowHeight = window.innerHeight;
var mouseHeightLocation = e.y;
var percentOfHeight = (100 / windowHeight) * mouseHeightLocation;
avatar.style.top = percentOfHeight + "%";
avatar.style.left = percentOfWidth + "%";
}
document.onmousemove = updateImgPosition;
This is a demo of what I have: http://jsfiddle.net/uQAmQ/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小提琴:http://jsfiddle.net/uQAmQ/2/
你不应在绝对定位元素上“平移”,因为窗口的宽度和高度会根据图像不断变化。更平滑的解决方案涉及使用背景图像。请参阅我的答案的中间部分以了解所使用的逻辑。
考虑这个 JavaScript(阅读评论;HTML + CSS at fiddle):
其背后的逻辑:
图像大小
除以窗口大小
。但是,这个比率不完整:图像会在窗口的左下角消失。要解决此问题,请从图像大小中减去窗口大小。结果可以在变量
ratioX
和ratioY
的代码中找到。<小时>
前面的代码必须在
window.onload
事件中加载,因为图像的大小是动态计算的。因此,正文中还包含一个 HTML 元素。通过在代码中指定背景的大小,可以将相同的代码写得更小、更高效。请参阅此改进的代码。 小提琴:http://jsfiddle.net/uQAmQ/3/
If you don't mind a decreased code readability, the following code is the best solution, Fiddle: http://jsfiddle.net/uQAmQ/4/:
Fiddle: http://jsfiddle.net/uQAmQ/2/
You should not "pan" on an absolutely positioned element, because the window's width and height keep changing according to the image. A smoother solution involves using a background image. See the middle of my answer for the used logic.
Consider this JavaScript (read comments; HTML + CSS at fiddle):
The logic behind it:
image size
divided bywindow size
.However, this ratio is not complete: The image would disappear at the bottom/left corner of the window. To fix this, substract the window's size from the image's size. The result can be found in the code at variable
ratioX
andratioY
.The previous code had to be loaded at the
window.onload
event, because the image's size was dynamically calculated. For this reason, a HTML element was also included in the body.The same code can be written much smaller and efficient, by specifying the size of the background in the code. See this improved code. Fiddle: http://jsfiddle.net/uQAmQ/3/
If you don't mind a decreased code readability, the following code is the best solution, Fiddle: http://jsfiddle.net/uQAmQ/4/: