代码之家  ›  专栏  ›  技术社区  ›  thanos_zach

带透明三角形的下拉列表(需要帮助)

  •  1
  • thanos_zach  · 技术社区  · 7 年前

    我想制作一个带有箭头的下拉按钮作为连接。我在网上找到了这个例子,它看起来很好,但我想修改它,以便扩展(全宽)分隔li项的行。正如您现在所注意到的,分隔li项目的线在左侧有一个间隙,因此它们不会扩展以适应边框的全宽。

    任何想法都将不胜感激!

    function DropDown(el) {
    				this.dd = el;
    				this.placeholder = this.dd.children('span');
    				this.opts = this.dd.find('ul.dropdown > li');
    				this.val = '';
    				this.index = -1;
    				this.initEvents();
    			}
    			DropDown.prototype = {
    				initEvents : function() {
    					var obj = this;
    
    					obj.dd.on('click', function(event){
    						$(this).toggleClass('active');
    						return false;
    					});
    
    					obj.opts.on('click',function(){
    						var opt = $(this);
    						obj.val = opt.text();
    						obj.index = opt.index();
    						obj.placeholder.text(obj.val);
    					});
    				},
    				getValue : function() {
    					return this.val;
    				},
    				getIndex : function() {
    					return this.index;
    				}
    			}
    
    			$(function() {
    
    				var dd = new DropDown( $('#dd') );
    
    				$(document).click(function() {
    					// all dropdowns
    					$('.wrapper-dropdown-3').removeClass('active');
    				});
    
    			});
    .wrapper-dropdown-3 {
        /* Size and position */
        position: relative;
        width: 200px;
        margin: 0 auto;
        padding: 10px;
    
        /* Styles */
        background: #fff;
        border-radius: 7px;
        border: 1px solid rgba(0,0,0,0.15);
        box-shadow: 0 1px 1px rgba(50,50,50,0.1);
        cursor: pointer;
        outline: none;
    
        /* Font settings */
        font-weight: bold;
        color: #8AA8BD;
    }
    
    .wrapper-dropdown-3:after {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        right: 15px;
        top: 50%;
        margin-top: -3px;
        border-width: 6px 6px 0 6px;
        border-style: solid;
        border-color: #8aa8bd transparent;
    }
    
    .wrapper-dropdown-3 .dropdown {
      /* Size & position */
        position: absolute;
        top: 140%;
        left: 0;
        right: 0;
    
        /* Styles */
        background: white;
        border-radius: inherit;
        border: 1px solid rgba(0,0,0,0.17);
        box-shadow: 0 0 5px rgba(0,0,0,0.1);
        font-weight: normal;
        transition: all 0.5s ease-in;
        list-style: none;
    
        /* Hiding */
        opacity: 0;
        pointer-events: none;
    }
    
    .wrapper-dropdown-3 .dropdown li a {
        display: block;
        padding: 10px;
        text-decoration: none;
        color: #8aa8bd;
        border-bottom: 1px solid #e6e8ea;
        box-shadow: inset 0 1px 0 rgba(255,255,255,1);
        transition: all 0.3s ease-out;
    }
    
    .wrapper-dropdown-3 .dropdown li i {
        float: right;
        color: inherit;
    }
    
    .wrapper-dropdown-3 .dropdown li:first-of-type a {
        border-radius: 7px 7px 0 0;
    }
    
    .wrapper-dropdown-3 .dropdown li:last-of-type a {
        border-radius: 0 0 7px 7px;
        border: none;
    }
    
    /* Hover state */
    
    .wrapper-dropdown-3 .dropdown li:hover a {
        background: #f3f8f8;
    }
    
    .wrapper-dropdown-3 .dropdown:after {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        bottom: 100%;
        right: 15px;
        border-width: 0 6px 6px 6px;
        border-style: solid;
        border-color: #fff transparent;    
    }
    
    .wrapper-dropdown-3 .dropdown:before {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        bottom: 100%;
        right: 13px;
        border-width: 0 8px 8px 8px;
        border-style: solid;
        border-color: rgba(0,0,0,0.1) transparent;    
    }
    
    .wrapper-dropdown-3.active .dropdown {
        opacity: 1;
        pointer-events: auto;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    
    <div id="dd" class="wrapper-dropdown-3" tabindex="1">
    	<span>Transport</span>
    	<ul class="dropdown">
    		<li><a href="#"><i class=""></i>Classic mail</a></li>
    		<li><a href="#"><i class=""></i>UPS Delivery</a></li>
    		<li><a href="#"><i class=""></i>Private jet</a></li>
    	</ul>
    </div>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Scath Adam Robinson    7 年前

    添加 padding:0; ul 以及 li 元素应该贯穿整个事物。

    function DropDown(el) {
    				this.dd = el;
    				this.placeholder = this.dd.children('span');
    				this.opts = this.dd.find('ul.dropdown > li');
    				this.val = '';
    				this.index = -1;
    				this.initEvents();
    			}
    			DropDown.prototype = {
    				initEvents : function() {
    					var obj = this;
    
    					obj.dd.on('click', function(event){
    						$(this).toggleClass('active');
    						return false;
    					});
    
    					obj.opts.on('click',function(){
    						var opt = $(this);
    						obj.val = opt.text();
    						obj.index = opt.index();
    						obj.placeholder.text(obj.val);
    					});
    				},
    				getValue : function() {
    					return this.val;
    				},
    				getIndex : function() {
    					return this.index;
    				}
    			}
    
    			$(function() {
    
    				var dd = new DropDown( $('#dd') );
    
    				$(document).click(function() {
    					// all dropdowns
    					$('.wrapper-dropdown-3').removeClass('active');
    				});
    
    			});
    .wrapper-dropdown-3 {
        /* Size and position */
        position: relative;
        width: 200px;
        margin: 0 auto;
        padding: 10px;
    
        /* Styles */
        background: #fff;
        border-radius: 7px;
        border: 1px solid rgba(0,0,0,0.15);
        box-shadow: 0 1px 1px rgba(50,50,50,0.1);
        cursor: pointer;
        outline: none;
    
        /* Font settings */
        font-weight: bold;
        color: #8AA8BD;
    }
    
    .wrapper-dropdown-3:after {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        right: 15px;
        top: 50%;
        margin-top: -3px;
        border-width: 6px 6px 0 6px;
        border-style: solid;
        border-color: #8aa8bd transparent;
    }
    
    .wrapper-dropdown-3 .dropdown {
      /* Size & position */
        position: absolute;
        top: 140%;
        left: 0;
        right: 0;
    
        /* Styles */
        background: white;
        border-radius: inherit;
        border: 1px solid rgba(0,0,0,0.17);
        box-shadow: 0 0 5px rgba(0,0,0,0.1);
        font-weight: normal;
        transition: all 0.5s ease-in;
        list-style: none;
    
        /* Hiding */
        opacity: 0;
        pointer-events: none;
    }
    
    .wrapper-dropdown-3 .dropdown li a {
        display: block;
        padding: 10px;
        text-decoration: none;
        color: #8aa8bd;
        border-bottom: 1px solid #e6e8ea;
        box-shadow: inset 0 1px 0 rgba(255,255,255,1);
        transition: all 0.3s ease-out;
    }
    
    .wrapper-dropdown-3 .dropdown li i {
        float: right;
        color: inherit;
    }
    
    .wrapper-dropdown-3 .dropdown li:first-of-type a {
        border-radius: 7px 7px 0 0;
    }
    
    .wrapper-dropdown-3 .dropdown li:last-of-type a {
        border-radius: 0 0 7px 7px;
        border: none;
    }
    
    /* Hover state */
    
    .wrapper-dropdown-3 .dropdown li:hover a {
        background: #f3f8f8;
    }
    
    .wrapper-dropdown-3 .dropdown:after {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        bottom: 100%;
        right: 15px;
        border-width: 0 6px 6px 6px;
        border-style: solid;
        border-color: #fff transparent;    
    }
    
    .wrapper-dropdown-3 .dropdown:before {
        content: "";
        width: 0;
        height: 0;
        position: absolute;
        bottom: 100%;
        right: 13px;
        border-width: 0 8px 8px 8px;
        border-style: solid;
        border-color: rgba(0,0,0,0.1) transparent;    
    }
    
    .wrapper-dropdown-3.active .dropdown {
        opacity: 1;
        pointer-events: auto;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    
    <div id="dd" class="wrapper-dropdown-3" tabindex="1">
    	<span>Transport</span>
    	<ul class="dropdown" style="padding:0;">
    		<li><a href="#"><i class=""></i>Classic mail</a></li>
    		<li><a href="#"><i class=""></i>UPS Delivery</a></li>
    		<li><a href="#"><i class=""></i>Private jet</a></li>
    	</ul>
    </div>