HTML、CSS、JavaScript 实现下拉菜单效果

JS

@font-face {
          font-family: 'Merriweather Sans';
          font-style: italic;
          font-weight: 800;
          src: local('Merriweather Sans ExtraBold Italic'), local('MerriweatherSans-ExtraBldItalic'), url(http://fonts.gstatic.com/s/merriweathersans/v5/nAqt4hiqwq3tzCecpgPmVfrUSW10CwTuVx9PepRx9ls.woff2) format('woff2'), url(http://fonts.gstatic.com/s/merriweathersans/v5/nAqt4hiqwq3tzCecpgPmVW2xy75WLVt7UI7Cycabsy8.woff) format('woff');
        }
        @font-face {
            font-family: "open";
            font-style: normal;
            font-weight: 300;
            src: local( "Open Sans Light" ), local( "OpenSans-Light" ), 
                url( https://themes.googleusercontent.com/static/fonts/opensans/v6/DXI1ORHCpsQm3Vp6mXoaTZ1r3JsPcQLi8jytr04NNhU.woff ) format( 'woff' );
        }
        
        *, *:before, *:after {
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
        }
        
        a {
            color: inherit;
            text-decoration: none;
        }
        
        h1, h2, h3 {
            margin: 0;
            font-weight: normal;
        }
        
        html, body {
            padding: 0;
            margin: 0;
            background: #eee;
        }
        
        body {
            padding-top: 80px;
        }
        
        .section {
            width: 400px;
            margin: 7px auto;
            height: 69px;
            box-shadow: 0 1px 2px rgba( 0, 0, 0, .2 );
            overflow: hidden;
            -webkit-transition: .35s;
            transition: .35s;
        }
        
        .title {
            padding: 20px;
            padding-top: 24px;
            background: #00C37E;
            color: #fff;
            cursor: pointer;
            text-shadow: 0 1px 0 #666;
            width: 100%;
            text-transform: capitalize;
            font-family: 'Merriweather Sans', sans-serif;
          font-style: italic;
            position: relative;
            -moz-user-select: none;
            -ms-user-select: none;
            -webkit-user-select: none;
            user-select: none;
            z-index: 10;
        }
        
        .title:before, .title:after {
            content: "";
            display: block;
            position: absolute;
            right: 20px;
            top: 21px;
          font-style: normal;
            height: 21px;
          line-height: 1;
            overflow: hidden;
            font-family: FontAwesome;
            font-size: 20px;
            background: #00C37E;
            -webkit-transition: .35s;
            transition: .35s;
        }
        
        .title:before {
            z-index: 2;
        }
        .title:after {
            top: 25px;
            -webkit-transform: rotate( 180deg );
            -ms-transform: rotate( 180deg );
            -moz-transform: rotate( 180deg );
            transform: rotate( 180deg );
        }
        
        .open .title:before {
            height: 0;
        }
        
        .body {
            font: 16px open, sans-serif;
            background: #fff;
            padding: 20px 20px 40px;
            color: #777;
            -moz-transform: translateY( -100% );
            -ms-transform: translateY( -100% );
            -webkit-transform: translateY( -100% );
            transform: translateY( -100% );
            overflow: hidden;
            -webkit-transition: .35s;
            transition: .35s;
        }
            .body h2 {
                color: #333;
                font-size: 22px;
                margin-bottom: 10px;
            }
            .body h2:before {
                content: '▪';
                padding-right: 7px;
                color: #00C37E;
            }
            .body a {
                color: #00C37E;
            }
            .body span {
                font-size: 12px;
            }
            
        .section.open {
            height: 288px;
        }
        .open .body {
            -webkit-transform: none;
            -ms-transform: none;
            -moz-transform: none;
            transform: none;
        }

JS部分

;!( function( w, d ) {
    
      'use strict';
      
        var titles = d.querySelectorAll( '.title' ),
            
            i = 0,
            len = titles.length;
            
        for ( ; i < len; i++ )
            titles[ i ].onclick = function( e ) {
            
                for ( var j = 0; j < len; j++ )
                    if ( this != titles[ j ] )
                        titles[ j ].parentNode.className = titles[ j ].parentNode.className.replace( / open/i, '' );
            
                var cn = this.parentNode.className;
            
                this.parentNode.className = ~cn.search( /open/i ) ? cn.replace( / open/i, '' ) : cn + ' open';
            
            };
    
    })( this, document );

HTML部分

<body>
        <div class="section">
            <div class="title">
                click here
            </div>
            <div class="body">
                <h2>你好</h2>
                <br />
                <br />
                <span>Crafted by: <a href="http://linkedin.com/in/mrReiha">Reiha Hosseini</a></span>
            </div>
        </div>
        <div class="section">
            <div class="title">
                click here
            </div>
            <div class="body">
                <h2>我不好</h2>
                <br />
                <br />
                <span>Crafted by: <a href="http://linkedin.com/in/mrReiha">Reiha Hosseini</a></span>
            </div>
        </div>
        <div class="section">
            <div class="title">
                click here
            </div>
            <div class="body">
                <h2>我很好</h2>
                <br />
                <br />
                <span>Crafted by: <a href="http://linkedin.com/in/mrReiha">Reiha Hosseini</a></span>
            </div>
        </div>
    </body>

效果图

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的HTMLJavaScript代码示例,用于实现菜单效果HTML代码: ``` <div class="dropdown"> <button onclick="showDropdown()" class="dropbtn">下菜单</button> <div id="myDropdown" class="dropdown-content"> <a href="#">选项1</a> <a href="#">选项2</a> <a href="#">选项3</a> </div> </div> ``` CSS代码: ``` /* 下菜单按钮样式 */ .dropbtn { background-color: #4CAF50; color: white; padding: 12px; font-size: 16px; border: none; cursor: pointer; } /* 下菜单内容样式 */ .dropdown-content { display: none; position: absolute; z-index: 1; } /* 下菜单选项样式 */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } /* 鼠标悬停时下菜单选项样式 */ .dropdown-content a:hover { background-color: #f1f1f1; } /* 显示下菜单时,修改按钮样式 */ .show { background-color: #3e8e41; } ``` JavaScript代码: ``` // 显示下菜单 function showDropdown() { document.getElementById("myDropdown").classList.toggle("show"); } // 关闭下菜单 window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); for (var i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } ``` 上述代码中,我们首先定义了一个`<div>`元素,包含一个按钮和一个下菜单。按钮的`onclick`属性绑定了一个JavaScript函数`showDropdown()`,用于显示下菜单。下菜单的内容通过`<a>`元素实现。 然后,我们定义了一些CSS样式,用于设置下菜单的外观。最后,我们编写了两个JavaScript函数,`showDropdown()`用于显示下菜单,`window.onclick`事件用于在用户点击页面其他区域时关闭下菜单。 注意:上述代码只是一个简单的示例,实际应用中可能需要根据具体要求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值