css3实现下拉框动画效果

本文介绍了一个使用HTML、CSS和jQuery实现的可交互下拉菜单,包括菜单的样式设计、交互逻辑及动画效果。

HTML:

<div class="content">
    <div class="select">
        <p>所有选项</p>
        <ul>
            <li data-value="所有选项" class="selected">所有选项</li>
            <li data-value="html">html</li>
            <li data-value="css">css</li>
            <li data-value="jQuery">jQuery</li>
            <li data-value="javascript">javascript</li>
        </ul>
    </div>
</div>

CSS:

body,p,ul {
    margin: 0;
    padding: 0;
}           
body {
    background-color: #0c9;
    color: #333;
}           
.content {
    padding-top: 5%;
}           
.content .select {
    width: 300px;
    height: 40px;
    margin: 0 auto;
    font-family: "Microsoft Yahei";
    font-size: 16px;
    background-color: #fff;
    position: relative;
}
/*transform(缩放、旋转、平移),显示的是最终效果,没有动画过程*/
/*transition是对元素本身的属性(比如:width、height)设置动画效果*/          
.content .select:after {
    content: '';
    display: block;
    width: 10px;
    height: 10px;
    border-left: 1px #ccc solid;
    border-bottom: 1px #ccc solid;
    position: absolute;
    top: 11px;
    right: 12px;
    transform: rotate(-45deg);
    transition: transform .3 ease-out,top .3s ease-out;
}           
.content .select p {
    padding: 0 15px;
    line-height: 40px;
    cursor: pointer;
}
/*设置下拉框收起时的高度过渡动画*/         
.content .select ul {
    list-style-type: none;
    background-color: #fff;
    width: 100%;
    overflow-y: auto;
    position: absolute;
    top: 40px;
    left: 0;
    max-height: 0;
    transition: max-height .3s ease-out;
}           
.content .select ul li {
    padding: 0 15px;
    line-height: 40px;
    cursor: pointer;
}           
.content .select ul li:hover {
    background-color: #e0e0e0;
}           
.content .select ul li.selected {
    background-color: #39f;
    color: #fff;
}
/*下拉框展开时调用动画slide-down*/
/*transform-origin设置缩放下拉框的基点位置*/            
.content .select.open ul {
    max-height: 250px;
    -webkit-animation: slide-down .5s ease-in;
    transition: max-height .3s ease-in;
    transform-origin: 50% 0; 
}
/*设置展开时下拉箭头的旋转动画*/          
.content .select.open:after {
    transform: rotate(-225deg);
    top: 18px;
    transition: all .3s ease-in-out;
}
/*为下拉框展开时添加名称为slide-down的关键帧动画*/
@-webkit-keyframes slide-down{
    0%{transform: scale(1,0);}
    25%{transform: scale(1,1.2);}
    50%{transform: scale(1,0.85);}
    75%{transform: scale(1,1.05);}
    100%{transform: scale(1,1);}
}

jQuery:

$(document).ready(function(){
    //点击P标签切换下拉框
    $('.select > p').on('click', function(event) {
        $('.select').toggleClass('open');
        event.stopPropagation();
    });
    //点击li标签,赋值、切换下拉框、给选中的li标签添加选中样式同级元素移除选中样式、阻止事件冒泡
    $('.select ul li').on('click',function(event){
        var _this = $(this);
        $('.select p').text(_this.attr('data-value'));
        $('.select').toggleClass('open');
        _this.addClass('selected').siblings().removeClass('selected');
        event.stopPropagation();
    })
    //点击除下拉框的其它地方,收起下拉框
    $(document).on('click',function(){
        $('.select').removeClass('open');
    })
});

效果:

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值