简体   繁体   English

基于光标位置的移动元素

[英]Moving element based on based on cursor position

I am working on an animation that slides a container based on the position of the mouse. 我正在制作一个动画,根据鼠标的位置滑动容器。

Basically looks like this: 基本上看起来像这样:

|--------------| <-- viewport
||-------------|--------| <- Image(
||             |        | 
||-------------|--------|
|--------------|

I'm listening to the window for a mousemove event and then translating the image -viewport.pageX. 我正在监听窗口中的mousemove事件,然后翻译图像-viewport.pageX。 Everything works and the image moves correctly, however, I seem to have a 'dead zone' on the image where the the mousemove event does nothing. 一切正常,图像正确移动,但是,我似乎在图像上有一个“死区”,鼠标移动事件什么都不做。 It seems like the dead zone is exactly the amount of the image that doesn't show. 似乎死区正好是未显示的图像数量。 I'm assuming there is some math to do with dividing that space in half and subtracting it from the amount I'm moving the image or something, but I can't quite figure it out. 我假设有一些数学可以将这个空间分成两半并从我正在移动图像的数量中减去它,但我无法弄明白。

Here is the code that captures the mouse position: 以下是捕获鼠标位置的代码:

$(window).on('mousemove', function(e) {
    globals.curPosX = (e.pageX).clamp(0, $('.home-wrapper .bg').width() - window.innerWidth);
    globals.curPosY = (e.pageY).clamp(0, $('.home-wrapper .bg').height() - window.innerHeight);
});

And the code that actually moves the image: 以及实际移动图像的代码:

draw: {
    start: function() {
        window.anim = requestAnimationFrame(draw.start);

        var content = document.getElementById('content');

        globals.tempPosX += (globals.curPosX - Mitek.home.globals.tempPosX) * 0.075;
        Mitek.home.globals.tempPosY += (globals.curPosY - globals.tempPosY) * 0.015;

        if ( has3d ) {
            content.style.WebkitTransform = 'translate3d(' + -globals.tempPosX + 'px, 0px, 0)';
            content.style.MozTransform = 'translate3d(' + -.globals.tempPosX + 'px, 0px, 0)';
            content.style.transform = 'translate3d(' + -.globals.tempPosX + 'px, 0px, 0)';
        } else {
            content.style.left = -globals.curPosX + 'px';
        }
    },
    stop: function() {
        cancelAnimationFrame(anim)
    }
}

Like I said, this all works well and looks good, I just can't figure out how to account for the extra space on the side. 就像我说的,这一切都运作良好,看起来很好,我只是无法弄清楚如何考虑侧面的额外空间。 Any suggestions? 有什么建议么?

Without a fiddle it's a bit difficult to improve your existing code, but I think there are more ways than one to achieve this effect. 没有小提琴,改进现有代码有点困难,但我认为有更多方法可以实现这种效果。

I worked on a project where we used CSS and jQuery to build a draggable viewport that revealed a portion of an image behind it. 我参与了一个项目,我们使用CSS和jQuery来构建一个可拖动的视口,它在其后面显示了一部分图像。 We used a non-jQueryUI drags() function found here on CSS-Tricks , but you could easily replace that with jQueryUI's draggable() . 我们在CSS-Tricks中使用了非jQueryUI drags drags()函数,但您可以使用jQueryUI的draggable()轻松替换它。 You could also use mousemove to animate the movement of the slider instead of dragging, even lock its location to the cursor's location so the cursor is always hovering over the visible portion. 您还可以使用mousemove为滑块的移动设置动画而不是拖动,甚至将其位置锁定到光标的位置,以便光标始终悬停在可见部分上。 You could also adapt this to work for vertical sliding, using blinders on the top and bottom and a bigger wrapper. 您还可以通过顶部和底部的遮挡以及更大的包装来调整它以进行垂直滑动。

Here's a fiddle demonstrating a slider that drags overtop an image, and reveals a section of it. 这是一个小提琴,展示了一个拖动图像的滑块,并显示了它的一部分。 It's a little hacky because you have to make sure the size of the blinders are large enough to obscure the image when the viewport is dragged to the left/right extremes, and the offset of the slider itself depends on the width of the blinders. 这有点hacky,因为当视口被拖动到左/右极端时,你必须确保遮挡的大小足以遮挡图像,并且滑块本身的偏移取决于遮挡的宽度。 This means it's good for a one-off solution or batch of images that are similarly sized, but as a dynamic solution for many image-size scenarios, this may not be what you need. 这意味着它适用于尺寸相似的一次性解决方案或一批图像,但作为许多图像大小场景的动态解决方案,这可能不是您所需要的。

I think this achieves the effect you're looking for with the above description though, LMK if this helps, and good luck. 我认为这可以通过上面的描述实现你正在寻找的效果,LMK如果这有帮助,祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM