I want to animate a div by mouse-over to change it's width. When mouse-leave, I want the div's width to be as it was before the animation.
Since I want this snippet to work with different div's (with different widths), I cant just write the width values but have to store the initial width inside a var.
The input for my var is 20%. It gets this value from my CSS file.
Var oldwidth = $tab.css('width')
After it animated the width to 10, it then animates the width back to var=oldwidth. Oldwidth should be back to 20% but it is now an integer value.
$("#zeile-1").mouseenter(function(event) {
$("#zeile-1 .tab.empty").each(function() {
var $tab = $(this)
if (this.hasAttribute("animating")) return
var oldWidth = $tab.css('width') //here the Var stores my Value (20%) from my css.//
$tab.attr("oldWidth", oldWidth)
$tab.attr("animating", "mouseenter")
$tab.animate({
width: "10"
}, 1000);
})
}).mouseleave(function() {
$("#zeile-1 .tab.empty").each(function() {
var $tab = $(this)
if ($tab.attr("animating") == "mouseleave") return
var oldWidth = $tab.attr("oldWidth")
$tab.attr("animating", "mouseleave")
$tab.animate({
width: oldWidth //here it outputs the value but as px//
}, {
duration: 1000,
complete: function() {
$tab.removeAttr("animating");
}
})
})
})
<body>
<div class="zeile" id="zeile-1">
<div class="a tab">31.</div>
<div class="b tab empty"> </div>
<div class="c tab">MAR</div>
<div class="d tab empty"> </div>
<div class="e tab empty"> </div>
<div class="f tab">201</div>
<div class="g tab empty"> </div>
<div class="h tab empty"> </div>
<div class="i tab">8</div>
</div>
.tab {
height: auto;
}
.a{
width: 11%;
}
.b{
width: 10%;
}
.c{
width: 17%;
}
.d{
width: 17%;
}
.e{
width: 17%;
and so on..
Can I tell the Var to store my value in % and to output it as %?