简体   繁体   English

使用CSS3 translate而不是偏移位置top / left

[英]Use CSS3 translate instead of offset position top/left

Is it possible to use CSS3 translate transforms with jQuery offset instead of top/left position? 是否可以使用CSS3翻译变换与jQuery偏移而不是顶部/左侧位置? I currently have the setup below, but it feels a little buggy and slow. 我目前有下面的设置,但它感觉有点马车和慢。 Any thoughts? 有什么想法吗?

$('.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
        });
    });
});

I thought this might've worked but it doesn't follow the mouse. 我认为这可能会奏效,但它不会跟随鼠标。

$(document).mousemove(function(e) {
    image.css({transform: 'translateX(' + e.pageX + 'px) translateY(' + e.pageY + 'px)'})
});

As mentioned in comments, the current code translates the element by the absolute no. 正如评论中所提到的,当前代码将元素转换为绝对数字。 of pixels and if the element is already positioned at an offset from (0,0) of the document (say due to extra elements before it or margin/padding etc) then there will be that much space between the mouse pointer and the actual position of the image. 如果元素已经位于文档的(0,0)偏移处(比如由于它之前的额外元素或边距/填充等),那么鼠标指针和实际位置之间会有很大的空间图像。

To avoid this, we must first pickup the original position of the element and then calculate the translate values with respect to that position. 为避免这种情况,我们必须首先拾取元素的原始位置,然后计算相对于该位置的平移值。 Below is a sample snippet. 以下是一个示例代码段。

 $(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> 


The vertical-align:top; vertical-align:top; on the img tag plays a key part in the above code because the default is baseline and since img is a replaced element the offset().top value seems to be the position of the baseline from the document (0,0) in pixels. img标签在上面的代码中起着关键作用,因为默认是baseline ,因为img是替换元素, offset().top值似乎是文档(0,0)中基线的位置(以像素为单位)。 This is what was causing the slight offset in the code I had provided in comments to the question. 这就是我在问题评论中提供的代码略微偏移的原因。 The problem can be seen in the below snippet. 问题可以在下面的代码片段中看到。

 $(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> 

Note: The above problem for some reason is seen only in Chrome. 注意: 出于某种原因,上述问题仅在Chrome中出现。


The below snippet is a demo of the how the works properly even when extra elements are present in the DOM above it. 下面的代码片段演示了即使在其上方的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> 


Another thing to note is that if there are multiple images prior to the current element in the DOM then it is best to calculate the offsets after those images are completely loaded. 另外需要注意的是,如果在DOM中的当前元素之前存在多个图像,则最好在完全加载这些图像之后计算偏移。 Else, the offset is just based on the line height of the element and not the actual height of the image. 否则,偏移量仅基于元素的线高,而不是图像的实际高度。 You can see the problem in the below snippet. 您可以在下面的代码段中看到问题。

 $(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> 

The below is the fixed version where the offsets are calculated on $(window).load() . 以下是固定版本,其中偏移量是在$(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> 

Note: All snippets are verified in Chrome v50 dev-m, Opera v35, Firefox 44.0.2, IE11, Edge 注意:所有代码段均在Chrome v50 dev-m,Opera v35,Firefox 44.0.2,IE11,Edge中经过验证

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

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