简体   繁体   English

获取背景位置

[英]Get background-position

I'm trying to animate a canvas on three different layers on the onmousedown and ontouchstart. 我正在尝试在onmousedown和ontouchstart的三个不同层上为画布设置动画。 The three canvas go from left to right at a different speed to give a perspective effect as long as the onmouseup or ontouchend are not fired. 只要不触发onmouseup或ontouchend,这三个画布将以不同的速度从左向右移动以提供透视效果。

I am using @-webkit-keyframes css to do my animations: 我正在使用@ -webkit-keyframes css来制作动画:

@-webkit-keyframes aCarsRight
{
from{background-position: 0px 0px;}
to{background-position: 640px 0px;}
}

.cars_move_right
{
-webkit-animation-name: aCarsRight;
-webkit-animation-duration: 5s;
-webkit-animation-iteration-count: 1;
-webkit-transition-timing-function: linear;
}

What I would like to do is get the current background-position of the three layers prior to disabling the pan animation and set them manually so when I remove the .cars_move_right class from my div is stays in the same position instead of rolling back to its default position. 我想做的是在禁用平移动画之前获取三层的当前背景位置并手动设置它们,因此当我从d​​iv中删除.cars_move_right类时,它会保持相同位置,而不是回滚到其原处。默认位置。

Is there a way to get the background-position or just a work around to get what I want? 有没有一种方法可以得到背景位置,或者只是解决我想要的东西? The page is meant for iPhone and iPod Touch. 该页面适用于iPhone和iPod Touch。

Thanks ! 谢谢 !


I found something interesting. 我发现了一些有趣的东西。 Instead of grabbing the background-position, stop the animation and set the background-position to my divs, I pause the animation. 我停止动画,而不是抓住背景位置,而是停止动画并将背景位置设置为div。

document.getElementById('global').style.webkitAnimationPlayState = 'paused';
document.getElementById('global_cars').style.webkitAnimationPlayState = 'paused';
document.getElementById('global_car').style.webkitAnimationPlayState = 'paused';

Haven't had the chance to test it on iPhone yet. 尚未有机会在iPhone上进行测试。


And, here's a method that seems to work cross-browswers to get any property: window.getComputedStyle(document.getElementById('div'))['background-position']; 而且,这是一种似乎可以跨浏览器工作以获取任何属性的方法:window.getComputedStyle(document.getElementById('div'))['background-position'];

Have you tried: var blah = ele.style.backgroundPosition; 您是否尝试过:var blah = ele.style.backgroundPosition;

That will return it like "10px 57px" 它将返回“ 10px 57px”

If the css rule has a hyphen in it then afaik in Javascript the hyphen is removed and camel case applied. 如果css规则中包含连字符,则Javascript中的afaik会删除连字符并应用驼峰式大小写。

I found that I can use this to get the property I couldn't get with [mydiv].style.[property] 我发现我可以使用它来获取[mydiv] .style。[property]无法获得的属性。

function getProperty(property, div)
{
    return window.getComputedStyle(document.getElementById(div))[property];
}

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

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