【发布时间】:2018-06-18 20:08:52
【问题描述】:
我想通过鼠标悬停动画来改变它的宽度。 当鼠标离开时,我希望 div 的宽度与动画之前一样。
由于我希望这个 sn-p 与不同的 div(具有不同的宽度)一起工作,我不能只写宽度值,而是必须将初始宽度存储在 var 中。
我的 var 的输入是 20%。它从我的 CSS 文件中获取这个值。
Var oldwidth = $tab.css('width')
将宽度设置为 10 后,将宽度设置为 var=oldwidth。 Oldwidth 应该回到 20%,但它现在是一个整数值。
$("#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..
我可以告诉 Var 将我的值存储在 % 中并将其输出为 % 吗?
【问题讨论】:
-
自定义属性应以
data-*开头,如data-oldWidth。为什么不直接使用 css#zeile-1 .tab.empty:hover?
标签: jquery html css output var