为什么鼠标光标和红线在将缩放 0.5 添加到正文标签时位置不同
why mouse cursor and red line not same position when add zoom 0.5 in to body tag?
为什么在将缩放 0.5 添加到正文标签时鼠标光标和红线的位置不同?
当在zoom 1;
上使用时,它工作得很好,
如何在zoom 0.5
上工作(缩放 1 除外;)?
https://jsfiddle.net/ksfqgv0p/6/
var isResizing = false;
$(function () {
var container = $('#container'),
left = $('#left'),
handle = $('#handle');
container.on('mousemove', function (e) {
isResizing = true;
});
container.on('mouseout', function (e) {
isResizing = false;
});
$(document).on('mousemove', function (e) {
if (!isResizing)
return;
left.css('width', e.clientX - container.offset().left);
handle.css('margin-left', e.clientX - container.offset().left);
});
});
问题是e.clientX
返回屏幕上的鼠标位置而不是对象上,因此将此值应用于width
属性,它比必须小 2 倍,因此您需要将此值除以缩放级别以获得该缩小对象上的正确鼠标位置。
喜欢这个:
left.css('width', ((e.clientX/$('body').css('zoom')) - container.offset().left));
handle.css('margin-left', ((e.clientX/$('body').css('zoom')) - container.offset().left));
这是jsFiddle
var isResizing = false;
$(function () {
var container = $('#container'),
left = $('#left'),
handle = $('#handle');
container.on('mousemove', function (e) {
isResizing = true;
});
container.on('mouseout', function (e) {
isResizing = false;
});
$(document).on('mousemove', function (e) {
if (!isResizing)
return;
console.log(container.offset().left);
console.log(e.clientX);
left.css('width', ((e.clientX/$('body').css('zoom')) - container.offset().left));
handle.css('margin-left', ((e.clientX/$('body').css('zoom')) - container.offset().left));
});
});
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container {
width: 100%;
height: 500px;
/* Disable selection so it doesn't get annoying when dragging. */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: moz-none;
-ms-user-select: none;
user-select: none;
}
#container #left {
position: absolute;
width: 750px;
height: 100%;
background-image: url("http://www.pilsnertop.com/wp-content/uploads/2015/12/Dog-Header3.jpg");
overflow: hidden;
background-repeat: no-repeat;
z-index: 9;
}
#container #right {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 100%;
background-image: url("http://www.twitrcovers.com/wp-content/uploads/2014/06/Dog-Butterfly-l.jpg");
overflow: hidden;
background-repeat: no-repeat;
}
#container #handle {
background: red;
height: 500px;
margin-left: 742px;
top: 0;
bottom: 0;
width: 8px;
cursor: w-resize;
position: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body style="
width: 100%;
height: 100%;
margin: 0;
padding: 0;
zoom:0.5;
"
>
<div id="container">
<!-- Left side -->
<div id="left">
<!-- Actual resize handle -->
<div id="handle"></div> This is the right side's content!
This is the left side's content! </div>
<!-- Right side -->
<div id="right">
</div>
</div>
</body>
相关文章:
- 正在尝试将标题标记添加到mailto链接,正文中包含URL
- 仅将元素附加到页面的正文标签,而不添加到iframe facebook、google+、twister
- 通过在 JavaScript 中使用正文标签的 ID 向
标签添加文本
- 获取IE版本+将类添加到正文
- jQuery 不将类添加到正文
- 添加事件处理程序以获取按钮单击LeadPage,我可以在其中对正文进行编码
- 页面加载后向<正文>添加元素时出现问题
- 如何在正文元素之前添加脚本
- 使用Mailto链接,如果可能的话,我可以在Outlook邮件正文中添加当前页面吗?在 asp.net
- 如何使用 JavaScript 在电子邮件正文中添加 HTML
- 将类添加到动态元素上的正文,在页面上的任意位置再次单击后删除
- 为什么鼠标光标和红线在将缩放 0.5 添加到正文标签时位置不同
- 使用 jQuery 将 CSS 添加到正文中的 head 标签
- 溢出隐藏添加在删除滚动条的正文标签中
- Javascript在mailto正文中添加换行符
- 如何将相同的页眉添加到每一页并将正文包裹在其中
- jQuery'css不是't向正文添加背景属性
- 使用chrome扩展,如何将html添加到页面的正文标记下方
- 我可以从Javascript函数向HTML正文添加图像吗
- 根据meta标签的内容向正文添加类