javascript - Jquery 动画动态宽度?

标签 javascript jquery css jquery-animate width

HTML

<div class="wrapper">
    <a href="#" class="tabs"><span>Tab-1 Text</span></a>
    <a href="#" class="tabs"><span>Tab-2 Text</span></a>
    <a href="#" class="tabs"><span>Tab-3 Text</span></a>
    <a href="#" class="tabs"><span>Tab-4 Text</span></a>
</div>

CSS

.wrapper{
    width:200px; 
    height:auto;  
    display:inline-block; 
    -webkit-border-bottom-right-radius: 3px;
    -webkit-border-top-right-radius: 3px;
    -moz-border-radius-bottomright: 3px;
    -moz-border-radius-topright: 3px;
    border-bottom-right-radius: 3px;
    border-top-right-radius: 3px;    
}
.wrapper .tabs{
    height:30px; 
    display:block; 
    -webkit-box-shadow: 1px 1px 6px #ccc inset;
    -moz-box-shadow: 1px 1px 6px #ccc inset;
    box-shadow: 1px 1px 6px #ccc inset; 
    -webkit-border-bottom-right-radius: 3px;        
    -webkit-border-top-right-radius: 3px;
    -moz-border-radius-bottomright: 3px;
    -moz-border-radius-topright: 3px;
    border-bottom-right-radius: 3px;
    border-top-right-radius: 3px;
    margin-bottom:1px;
    border-top:1px solid #ccc;
    border-bottom:1px solid #ccc;
    border-right:1px solid #ccc;
    text-decoration:none; 
    font-size:11px; 
    line-height:30px; 
    overflow:hidden;
    width:30px;
}
.wrapper .tabs span{    
    padding-left:35px;
    color:#000000;
    font-weight:bold;
    height:30px;
    display:block;
    width:auto;
 }

JQUERY

$(".wrapper").on("mouseenter",".tabs",function(){
    $(this).stop().animate({
        width:"202px",
        duration:"fast"                 
});     
}).on("mouseleave",".tabs",function(){
    $(".tabs").stop().animate({
        width:"30px",
        duration:"fast"
    });
});

JsFiddle

http://jsfiddle.net/PMBxT/

当悬停任何选项卡时,选项卡会拉伸(stretch)并显示隐藏文本,但如果我将动画宽度选项更改为“自动”,效果将不起作用。我该如何解决这个问题?

最佳答案

可能是这样,设置一个隐藏span的内容来得到对应的宽度:

http://jsfiddle.net/PMBxT/1/

$(".wrapper").on("mouseenter",".tabs",function(){
    $('#hide').html(this.innerHTML);
    $(this).stop().animate({
        width:$('#hide').width(),
        duration:"fast"                 
    });     
}).on("mouseleave",".tabs",function(){
    $(".tabs").stop().animate({
        width:"30px",
        duration:"fast"
    });
});

关于javascript - Jquery 动画动态宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16965362/

相关文章:

javascript - React-select:背景颜色未填满 wordWrap :"scroll"上的全宽

javascript - 如何避免网站其他部分出现 jQuery 移动样式?

jquery 幻灯片中的上一个和下一个按顺序重复

javascript - 如何在使用 jquery 点击超链接时更改页面上的墙纸?

javascript - 阻止网页页面跳转到顶部

html - 导航菜单在表格的 td 内对齐且无边框

javascript - 我可以在同一个 React 代码库中同时使用 ES6 和 ES5 吗?

javascript - 如何使用 Vue.js mixins 替换字符串值?

javascript - Angular Material 表单元格格式

css - DC.js 表数据显示不需要的额外行