我刚开始玩CSS。我正在尝试制作一个应该在单击按钮时触发的动画(下拉菜单)。我找到了一个很好的例子,但是在 hoover 事件的触发器中并改变了几个 div 的样式。
我想我需要在单击按钮时设置样式,然后在再次单击时将其更改回来。但我无法让它工作。
这是CSS:
/* Static state */
#container {
width: 400px;
height: 400px;
position: relative;
border: 1px solid #ccc;
}
.parent1 {
/* overall animation container */
height: 0;
overflow: hidden;
-webkit-transition-property: height;
-webkit-transition-duration: .5s;
-webkit-perspective: 1000px;
-webkit-transform-style: preserve-3d;
-moz-transition-property:height;
-moz-transition-duration: .5s;
-moz-perspective: 1000px;
-moz-transform-style: preserve-3d;
-o-transition-property: all;
-o-transition-duration: .5s;
-o-transform: rotateX(-90deg);
-o-transform-origin: top;
transition-property: height;
transition-duration: .5s;
perspective: 1000px;
transform-style: preserve-3d;
}
.parent2 {
/* full content during animation *can* go here */
}
.parent3 {
/* animated, "folded" block */
height: 56px;
-webkit-transition-property: all;
-webkit-transition-duration: .5s;
-webkit-transform: rotateX(-90deg);
-webkit-transform-origin: top;
-moz-transition-property: all;
-moz-transition-duration: .5s;
-moz-transform: rotateX(-90deg);
-moz-transform-origin: top;
-o-transition-property: all;
-o-transition-duration: .5s;
-o-transform: rotateX(-90deg);
-o-transform-origin: top;
transition-property: all;
transition-duration: .5s;
transform: rotateX(-90deg);
transform-origin: top;
}
/* Hover states to trigger animations */
#container:hover .parent1 { height: 111px; }
#container:hover .parent3 {
-webkit-transform: rotateX(0deg);
-moz-transform: rotateX(0deg);
-o-transform: rotateX(0deg);
transform: rotateX(0deg);
height: 111px;
}
这就是我一直在做的事情......
<script>
function fold()
{
var element = document.getElementById('parent1');
element.style.height= 111;
element = document.getElementById('parent3');
var prop = getTransformProperty(element);
element.style[prop] = 'rotateX(0deg)';
element.style.height= 111;
}
function getTransformProperty(element)
{
// Note that in some versions of IE9 it is critical that
// msTransform appear in this list before MozTransform
var properties = [
'transform',
'WebkitTransform',
'msTransform',
'MozTransform',
'OTransform'
];
var p;
while (p = properties.shift()) {
if (typeof element.style[p] != 'undefined') {
return p;
}
}
return false;
}
</script>
有任何想法吗?