简体   繁体   English

具有background-size:cover的可拖动背景图像

[英]Draggable background-image with background-size:cover

I have a fixed div that contains an image as a background like this 我有一个固定的div,其中包含一个像这样的背景图像

<div style='
     width:230px;
     height:230px;
     background-repeat: no-repeat;
     background-image:url(img/myimage.jpeg)
     background-size: cover;'></div>

The image overlaps the div-box. 图像与div框重叠。 The user should be able to drag the image and the new background-position: coordinates should be accessible somehow via jQuery just after the user releases the mouse. 用户应该能够拖动图像和新的背景位置:在用户释放鼠标之后,应该可以通过jQuery以某种方式访问​​坐标。 My aim is to create a very simple crap image effect just like in facebook: 我的目标是创建一个非常简单的废话图片效果,就像在Facebook中一样:

在此处输入图片说明

How can I achive this? 我该如何实现? And is it even possible to show the overflowig background with background-size:cover? 甚至有可能用background-size:cover显示溢出背景吗?

You can use this Plugin , and try 您可以使用此插件 ,然后尝试

element.backgroundDraggable()

Here you have a demo 这里有一个演示

You need to handle the mousemove event on the div, and calculate the new background-position from there. 您需要处理div上的mousemove事件,并从那里计算新的background-position

Here is an example to get you started: http://codepen.io/dghez/pen/ItxKE 这是一个入门的示例: http : //codepen.io/dghez/pen/ItxKE

To see if the left mouse button is pressed within the mousemove handler (to simulate a drag), check e.buttons === 1 ( MDN ) 要查看是否在mousemove处理程序中按下了鼠标左键(模拟拖动),请检查e.buttons === 1MDN

Update: 更新:

Modified mousemove handler from the example, which lets you drag the background (while annoyingly selecting the text at the same time...): 该示例中的修改过的mousemove处理程序,可让您拖动背景(同时烦人地选择文本...):

...
$(".title").mousemove(function(e) {
    if(e.buttons === 1) {
        mouseX = e.offsetX;
        mouseY = e.offsetY;
        traX = mouseX + 1640;
        traY = mouseY + 200;
        //console.log(traX, traY);
        $(".title").css({"background-position": traX + "px " + traY + "px"});
    }
});

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

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