[英]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.