问题
I have a series of divs that slide down using -webkit-transform
from a negative margin-top to a position on the screen. Problem is that they end up where they would normally be sitting if there was no negetive margin-top applied to them.
This means if I show the 2nd div it has an empty space the size of the first div above it. To solve this I can apply a negative margin-top to the 2nd div, but I think thats messy.
My main concern is that if the height of dropdiv1 changes then I would have to reset the values on the click functions to have the other divs line up correctly again when shown.
Is there a way to amend -webkit-transform
to incorporate postion:absolute?
My current code is:
CSS:
#dropdiv1 {
-webkit-transform: translate(0, -3000px);
-webkit-transition: all ease-in 1s;
}
#dropdiv2 {
-webkit-transform: translate(0, -3400px);
-webkit-transition: all ease-in 1s;
}
#dropdiv3 {
-webkit-transform: translate(0, -4200px);
-webkit-transition: all ease-in 1s;
}
JQuery:
$('#clickme1').click(
function() {
$('#dropdiv1').css('-webkit-transform','translate(0, -335px)');
});
$('#clickme2').click(
function() {
$('#dropdiv2').css('-webkit-transform','translate(0, -2335px)');
});
$('#clickme3').click(
function() {
$('#dropdiv3').css('-webkit-transform','translate(0, -3300px)');
});
HTML:
<ul class="mainmenu">
<li><a id="clickme1" href="#">Click Me 1</a></li>
<li><a id="clickme2" href="#">Click Me 2</a></li>
<li><a id="clickme3" href="#">Click Me 3</a></li>
</ul>
<div class="showdata" id="dropdiv1">
Lots of random text....
</div>
<div class="showdata" id="dropdiv2">
Lots of random text....
</div>
<div class="showdata" id="dropdiv3">
Lots of random text....
</div>
回答1:
I don't really see any logical behavior for animating position: absolute
.
This would be more easy to read:
#dropdiv1 {
-webkit-transform: translate(0, -3000px);
-webkit-transition: all ease-in 1s;
}
#dropdiv1.anim {
-webkit-transform: translate(0, -335px);
}
#dropdiv2 {
-webkit-transform: translate(0, -3400px);
-webkit-transition: all ease-in 1s;
}
#dropdiv2.anim {
-webkit-transform: translate(0, -2335px);
-webkit-transition: all ease-in 1s;
}
#dropdiv3 {
-webkit-transform: translate(0, -4200px);
-webkit-transition: all ease-in 1s;
}
#dropdiv3.anim {
-webkit-transform: translate(0, -3300px);
-webkit-transition: all ease-in 1s;
}
Then add class instead.
$('#dropdiv3').addClass('anim');
That, or either run a .offset()
before applying position: absolute
and use that top
and left
for animation.
回答2:
OK I have found a kind of solution. Still messy but ok:
<div id="bigcontainer">
<ul class="mainmenu">
<li><a id="clickme1" href="#">Click Me 1</a></li>
<div class="showdata" id="dropdiv1">
Lots of random text....
</div>
<li><a id="clickme2" href="#">Click Me 2</a></li>
<div class="showdata" id="dropdiv2">
Lots of random text....
</div>
<li><a id="clickme3" href="#">Click Me 3</a></li>
<div class="showdata" id="dropdiv3">
Lots of random text....
</div>
</ul>
</div>
CSS:
#bigcontainer {
height: 1500px;/*as big as the bigest dropdiv to enable scrolling*/
}
#dropdiv1 {
-webkit-transform: translate(0, -3000px);
-webkit-transition: all ease-in 1s;
z-index: 1;
position: absolute;
}
#dropdiv2 {
-webkit-transform: translate(0, -3400px);
-webkit-transition: all ease-in 1s;
z-index: 2;
position: absolute;
}
#dropdiv3 {
-webkit-transform: translate(0, -4200px);
-webkit-transition: all ease-in 1s;
z-index: 3;
position: absolute;
}
Not particularly a fan but it will do!
来源:https://stackoverflow.com/questions/11202076/webkit-transform-using-positionabsolute