简体   繁体   English

Javascript:将Transform Scale考虑在内的准确CSS TranslateX(位置)

[英]Javascript : accurate CSS TranslateX ( position ) taking Transform Scale into account

I'm scaling a div.parent by n% using transform : scale( 0.25 ) . 我正在使用transform : scale( 0.25 )div.parent缩放n%。 The width of div.parent is quite large and exceeds the viewport bounds. div.parent的宽度非常大,超过了视口范围。 This container div has n children, each with a relative offset position, which I'm getting via jQuery like ( approximation ) 这个容器div有n个子对象,每个子对象都有一个相对的偏移位置,我通过jQuery像(近似)

parent  = $ '.parent'
child   = $ '.child'
posLeft = child.position().left  

I'm then moving the div.parent container using the value posLeft . 然后,我使用值posLeft移动div.parent容器。

 parent.css
    transform : "translate3d( #{posLeft}px, 0, 0 )"

I would like to expand the function that handles the above animation by allowing it to continue to move div.parent accurately ( as related to the child items posLeft ) when it's scaled down. 我想扩展其处理上述动画的function ,方法是使其按比例缩小时继续准确地移动 div.parent (与子项posLeft )。 Here's an image to help illuminate the problem. 这是一张有助于阐明问题的图像。

在此处输入图片说明

The solution turned out to be really simple — after eliminating a few bugs in my code. 在消除了我的代码中的一些错误之后,该解决方案实际上非常简单。 For anyone else who is interested in doing something similar, I'll post an approximation of the code below ( which uses TweenMax to handle the transitions ) 对于其他有兴趣做类似事情的人,我将在下面发布近似的代码(使用TweenMax来处理过渡)

 _state = 
     scale : 1

 state: ( state, value )-> 
     if value is undefined
         return _state[state] 
     else 
         _state[state] = value 

 calculateDistance( distance, scale )->
     newDistance = distance * scale
     return newDistance 


 scaleDown = ( amount )->
     if amount > 1 then amount = 1

     props =
         x                   : 0 
         scale               : amount 
         transformOrigin     : 'center left'
         onComplete          : Done 

         TweenMax.to container, 0.4, props 

  Done =->
      num = this.vars.css.scale 

      state( 'scale', num )

and how to use calculateDistance() 以及如何使用calculateDistance()

location =  calculateDistance( move.destination.attr( 'data-location' ), state('scale' ) )

This enables me to use 1 method for both the scaled and full versions of any transitions. 这使我可以对任何转换的缩放版本和完整版本都使用1方法。

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

相关问题 CSS 变换:translateX(向右); - CSS transform: translateX( to the right ); CSS转换比例和JavaScript offsetHeight - CSS transform scale and Javascript offsetHeight CSS比例转换影响位置计算 - CSS scale transform affecting position calculations Javascript + CSS:从绝对定位的元素转换为CSS转换(左,上,宽,高以转换为X,translateY,比例) - Javascript + CSS: Converting from absolute positioned elements to CSS Transforms (left, top, width, height to translateX, translateY, scale) CSS转换:iOS上的translationX Flash错误 - CSS transform: translateX flash bug on iOS CSS 变换:translateX animation 渲染问题 - CSS transform:translateX animation render problems 使用javascript,范围输入和CSS转换的数学:scale() - Mathematics with javascript, range input, and css transform: scale() jQuery“ position()。top”和css3“ transform:scale()”之间的冲突 - Conflict between jquery “position().top” and css3 “transform: scale()” 是否可以在单个 CSS 过渡中结合 translateX/translateY 和 scale? - Is it possible to combine translateX/translateY and scale in single CSS transition? 如何使用JavaScript更改transform属性的translateX()值? - How do I change the translateX() value of the transform property using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM