使用 CSS3 平移而不是顶部/左侧偏移位置
Use CSS3 translate instead of offset position top/left
是否可以使用带有jQuery偏移量而不是顶部/左侧位置的CSS3翻译转换?我目前有下面的设置,但感觉有点错误和缓慢。有什么想法吗?
$('.artists-list-container ul li a').on('mouseenter', function() {
var image = $(this).parent('li').find('.image-container');
$(this).parent('li').addClass('active');
image.show();
$(document).mousemove(function(e) {
image.offset({
left: e.pageX,
top: e.pageY
});
});
});
我认为这可能有效,但它不会跟随鼠标。
$(document).mousemove(function(e) {
image.css({transform: 'translateX(' + e.pageX + 'px) translateY(' + e.pageY + 'px)'})
});
如注释中所述,当前代码将元素转换为绝对编号。 像素,如果元素已经位于文档 (0,0( 的偏移量处(例如由于它之前的额外元素或边距/填充等(,那么鼠标指针和图像的实际位置之间将有那么大的空间。
为了避免这种情况,我们必须首先选取元素的原始位置,然后计算相对于该位置的平移值。下面是一个示例片段。
$(document).ready(function() {
var image = $('li').find('.image-container');
var position = image.offset();
$(document).mousemove(function(e) {
image.css({
transform: 'translateX(' + (e.pageX - position.left) + 'px) translateY(' + (e.pageY - position.top) + 'px)'
})
});
})
ul {
list-style-type: none;
}
img {
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<img class='image-container' src='http://lorempixel.com/200/200/nature/1' />
</li>
</ul>
img
标记上的vertical-align:top;
在上面的代码中起着关键作用,因为默认值是baseline
的,并且由于img
是替换的元素,因此offset().top
值似乎是基线相对于文档的位置 (0,0( 以像素为单位。这就是导致我在问题注释中提供的代码略有偏移的原因。问题可以在下面的代码片段中看到。
$(document).ready(function() {
var image = $('li').find('.image-container');
var position = image.offset();
$(document).mousemove(function(e) {
image.css({
transform: 'translateX(' + (e.pageX - position.left) + 'px) translateY(' + (e.pageY - position.top) + 'px)'
})
});
})
ul {
list-style-type: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<img class='image-container' src='http://lorempixel.com/200/200/nature/1' />
</li>
</ul>
注意: 由于某种原因,上述问题仅在 Chrome 中出现。
下面的代码片段演示了即使上面的 DOM 中存在额外的元素,它如何正常工作。
$(window).load(function() {
var image = $('li').find('.image-container');
var position = image.offset();
$(document).mousemove(function(e) {
image.css({
transform: 'translateX(' + (e.pageX - position.left) + 'px) translateY(' + (e.pageY - position.top) + 'px)'
})
});
})
ul {
list-style-type: none;
}
img {
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Some content before the image</div>
<p>Some other content before the image</p>
<ul>
<li>
<img class='image-container' src='http://lorempixel.com/200/200/nature/1' />
</li>
</ul>
另一件需要注意的事情是,如果在 DOM 中的当前元素之前有多个图像,那么最好在这些图像完全加载后计算偏移量。否则,偏移量仅基于元素的行高,而不是图像的实际高度。您可以在下面的代码片段中看到问题。
$(document).ready(function() {
var image = $('li').find('.image-container');
var position = image.offset();
$(document).mousemove(function(e) {
image.css({
transform: 'translateX(' + (e.pageX - position.left) + 'px) translateY(' + (e.pageY - position.top) + 'px)'
})
});
})
ul {
list-style-type: none;
}
img {
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Some content before the image</div>
<p>Some other content before the image</p>
<img src='http://lorempixel.com/100/100/nature/1' />
<ul>
<li>
<img class='image-container' src='http://lorempixel.com/200/200/nature/1' />
</li>
</ul>
以下是固定版本,其中偏移量在$(window).load()
上计算。
$(window).load(function() {
var image = $('li').find('.image-container');
var position = image.position();
$(document).mousemove(function(e) {
image.css({
transform: 'translateX(' + (e.pageX - position.left) + 'px) translateY(' + (e.pageY - position.top) + 'px)'
})
});
})
ul {
list-style-type: none;
}
img {
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Some content before the image</div>
<p>Some other content before the image</p>
<img src='http://lorempixel.com/100/100/nature/1' />
<ul>
<li>
<img class='image-container' src='http://lorempixel.com/200/200/nature/1' />
</li>
</ul>
注意:所有代码段均在Chrome v50 dev-m,Opera v35,Firefox 44.0.2,IE11,Edge中进行了验证
相关文章:
- 使用jQuery更改元素的顶部位置
- 页面滚动在粘性页脚布局中保持滚动顶部位置
- 点击更改顶部位置不会有任何作用
- 打开对话框正在更改我的滚动顶部位置
- 获取文档的当前顶部位置
- 单击时通过动画更改元素的顶部位置
- jQuery在某些动画更改其位置后获取元素的最新顶部位置
- 单击时使用旧的jQuery滚动顶部位置
- 防止 iframe 中的恶意 Flash 广告将用户重定向到顶部位置
- 更改borderBottom在顶部位置地址的颜色
- Jquery点击事件只在页面到达顶部位置时起作用
- Jquery注意,右顶部位置,显示新消息更高(又名后进先出)
- 变换(缩放)与简单的宽度/高度和左/顶部位置
- 如何获得文本本身的顶部位置,而不是文本元素,在jQuery/javascript
- 设置顶部位置为父元素与子元素之差的1/2
- Chart.js,每次更新后,避免滚动到顶部位置
- 如何找到窗口顶部位置和我的iframe开始位置之间的距离
- jQuery -拖放-获取左侧和顶部位置
- 基于顶部位置更新Div的类名
- 设置顶部位置并打开新选项卡