如何将border-radius添加到下拉菜单中

时间:2015-08-08 20:36:07

标签: javascript jquery html css css3

我无法将border-radius添加到整个下拉菜单中,如果我将border-radius添加到click-nav ul li,则将radius应用于列表项。我已将border-radius添加到.click-nav ul,但半径未应用于整个下拉菜单。

$(function () {
  $('.click-nav > ul').toggleClass('no-js js');
  $('.click-nav .js ul').hide();
  $('.click-nav .js').click(function(e) {
    $('.click-nav .js ul').slideToggle(200);
    $('.clicker').toggleClass('active');
    e.stopPropagation();
  });
  $(document).click(function() {
    if ($('.click-nav .js ul').is(':visible')) {
      $('.click-nav .js ul', this).slideUp();
      $('.clicker').removeClass('active');
    }
  });
});
body{background:#000}

.click-nav {width:200px;}

.click-nav ul {position:relative;}

.click-nav ul li {position:relative;list-style:none;cursor:pointer;}

.click-nav ul li ul {position:absolute;left:0;right:0;margin:5px 0px 0px -40px;}

.arrow-up {
	width: 0; 
	height: 0; 
	border-left: 10px solid transparent;
	border-right: 10px solid transparent;
	border-bottom: 10px solid white;
  margin-left:30px;
  }


.click-nav ul .clicker {position:relative;border:2px solid #fff;color:#fff;border-radius:6px;background-color: rgba(255,255,255,0);width:70px;}

.click-nav ul .clicker:hover,.click-nav ul .active {background-color: rgba(255,255,255,0.2);width:70px;}


.click-nav ul li a {
  background:#fff;transition:background-color 0.2s ease-in-out;
-webkit-transition:background-color 0.2s ease-in-out;
-moz-transition:background-color 0.2s ease-in-out;
  display:block;
  padding:6px 10px 6px 10px;color:#333;text-decoration:none;}

.click-nav ul li a:hover {background-color: rgba(214,247,255,1); }
.click-nav .no-js ul {display:none;}
.click-nav .no-js:hover ul {display:block;}
<script src="https://code.jquery.com/jquery-1.8.3.js"></script><div class="click-nav">
  <ul class="no-js">
    <li>
      <a href="#" class="clicker">Categories</a>
      <ul>
        <li><div class="arrow-up"></div></li>
        <li><a href="#">Dashboard</a></li>
        <li><a href="#">Settings</a></li>
        <li><a href="#">Privacy</a></li>
        <li><a href="#">Help</a></li>
        <li><a href="#">Sign out</a></li>
      </ul>
    </li>
  </ul>
</div>

5 个答案:

答案 0 :(得分:7)

尝试将类添加到第一个和最后一个列表元素...

CSS

sentResult

HTML

.first a {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
}

.last a{
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
}

演示:http://jsfiddle.net/vp9eykne

答案 1 :(得分:4)

您的border-radius正在设置,刚刚正确显示。如果您查看this code,我已添加半径并设置background-color: red;。在这里,您将看到第二个ul的位置以及半径的显示方式。

要解决此问题,您可以为列表的第一个(技术上,第二个)和最后一个元素添加类或伪类。

.click-nav ul li ul li:nth-child(2) a {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

.click-nav ul li ul li:last-child a {
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
}

以下是working version的链接。如果您不想使用伪类,请重新编写您的下拉列表,以使内部ul的顶部箭头部分没有被推到margin并且string str01 = GetEscapeSequence(c1); 未被推到目前为止。

答案 2 :(得分:4)

当您向列表(ul)添加border-radius属性时,您看不到任何内容。

您还必须添加overflow: hidden。 ul的默认填充左值也会导致问题。您必须修复它,也添加padding-left: 0。最后你的箭头会导致边界半径问题。您必须将border-radius添加到列表的第二个li。

你可以看到我的小提琴,我已经完成了它。

http://jsfiddle.net/naufu5hk/3/

答案 3 :(得分:2)

尝试使用选择器.click-nav ul .clicker + ul li:nth-of-type(2) a选择li a元素后面的第一个.click-nav ul .clicker元素,将border-top-left-radiusborder-top-right-radius设置为6px; .click-nav ul .clicker + ul li:nth-last-of-type(1) a选择li a后的最后.click-nav ul .clicker,将border-bottom-left-radiusborder-bottom-right-radius设置为6px

.click-nav ul .clicker + ul li:nth-of-type(2) a {
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  border-bottom-right-radius: 0px;
  border-bottom-left-radius: 0px;
}

.click-nav ul .clicker + ul li:nth-last-of-type(1) a {
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  border-bottom-right-radius: 6px;
  border-bottom-left-radius: 6px;
}

&#13;
&#13;
$(function() {
  $('.click-nav > ul').toggleClass('no-js js');
  $('.click-nav .js ul').hide();
  $('.click-nav .js').click(function(e) {
    $('.click-nav .js ul').slideToggle(200);
    $('.clicker').toggleClass('active');
    e.stopPropagation();
  });
  $(document).click(function() {
    if ($('.click-nav .js ul').is(':visible')) {
      $('.click-nav .js ul', this).slideUp();
      $('.clicker').removeClass('active');
    }
  });
});
&#13;
body {
  background: #000
}
.click-nav {
  width: 200px;
}
.click-nav ul {
  position: relative;
}
.click-nav ul li {
  position: relative;
  list-style: none;
  cursor: pointer;
}
.click-nav ul li ul {
  position: absolute;
  left: 0;
  right: 0;
  margin: 5px 0px 0px -40px;
}
.arrow-up {
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-bottom: 10px solid white;
  margin-left: 30px;
}
.click-nav ul .clicker + ul li:nth-of-type(2) a {
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  border-bottom-right-radius: 0px;
  border-bottom-left-radius: 0px;
}

.click-nav ul .clicker + ul li:nth-last-of-type(1) a {
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  border-bottom-right-radius: 6px;
  border-bottom-left-radius: 6px;
}
.click-nav ul .clicker {
  position: relative;
  border: 2px solid #fff;
  color: #fff;
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0);
  width: 70px;
}
.click-nav ul .clicker:hover,
.click-nav ul .active {
  background-color: rgba(255, 255, 255, 0.2);
  width: 70px;
}
.click-nav ul li a {
  background: #fff;
  transition: background-color 0.2s ease-in-out;
  -webkit-transition: background-color 0.2s ease-in-out;
  -moz-transition: background-color 0.2s ease-in-out;
  display: block;
  padding: 6px 10px 6px 10px;
  color: #333;
  text-decoration: none;
}
.click-nav ul li a:hover {
  background-color: rgba(214, 247, 255, 1);
}
.click-nav .no-js ul {
  display: none;
}
.click-nav .no-js:hover ul {
  display: block;
}
&#13;
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<div class="click-nav">
  <ul class="no-js">
    <li>
      <a href="#" class="clicker">Categories</a>
      <ul>
        <li>
          <div class="arrow-up"></div>
        </li>
        <li><a href="#">Dashboard</a>
        </li>
        <li><a href="#">Settings</a>
        </li>
        <li><a href="#">Privacy</a>
        </li>
        <li><a href="#">Help</a>
        </li>
        <li><a href="#">Sign out</a>
        </li>
      </ul>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:2)

您的菜单由&lt; li&gt; 元素组成,因此您必须为第一个和最后一个&lt; li&gt; 元素添加边框半径。

但是你没有为li元素添加背景(可能更容易?) 所以你必须选择菜单: li元素的css选择器:

ul li:first-of-type { ... }

您所做的是将背景添加到li elements child:

ul li:first-of-type *:first-of-type { ... }

我们需要结束,所以我们最后一个元素,但它的孩子:

ul li:last-of-type *:last-of-type { ... }

&#13;
&#13;
$(function() {
  $('.click-nav > ul').toggleClass('no-js js');
  $('.click-nav .js ul').hide();
  $('.click-nav .js').click(function(e) {
    $('.click-nav .js ul').slideToggle(200);
    $('.clicker').toggleClass('active');
    e.stopPropagation();
  });
  $(document).click(function() {
    if ($('.click-nav .js ul').is(':visible')) {
      $('.click-nav .js ul', this).slideUp();
      $('.clicker').removeClass('active');
    }
  });
});
&#13;
body {
  background: #000
}
.click-nav {
  width: 200px;
}
.click-nav ul {
  position: relative;
}
.click-nav ul li {
  position: relative;
  list-style: none;
  cursor: pointer;
}
.click-nav ul li ul {
  position: absolute;
  left: 0;
  right: 0;
  margin: 5px 0px 0px -40px;
}
.arrow-up {
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-bottom: 10px solid white;
  margin-left: 30px;
}
.click-nav ul .clicker {
  position: relative;
  border: 2px solid #fff;
  color: #fff;
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0);
  width: 70px;
}
.click-nav ul .clicker:hover,
.click-nav ul .active {
  background-color: rgba(255, 255, 255, 0.2);
  width: 70px;
}
.click-nav ul li a {
  background: #fff;
  transition: background-color 0.2s ease-in-out;
  -webkit-transition: background-color 0.2s ease-in-out;
  -moz-transition: background-color 0.2s ease-in-out;
  display: block;
  padding: 6px 10px 6px 10px;
  color: #333;
  text-decoration: none;
}
.click-nav ul li a:hover {
  background-color: rgba(214, 247, 255, 1);
}
.click-nav .no-js ul {
  display: none;
}
.click-nav .no-js:hover ul {
  display: block;
}
.menu li:first-of-type *:first-of-type {
  border-top-left-radius: 7px;
  border-top-right-radius: 7px;
}
.menu li:last-of-type *:last-of-type {
  border-bottom-left-radius: 7px;
  border-bottom-right-radius: 7px
}
&#13;
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<div class="click-nav">
  <ul class="no-js">
    <li>
      <a href="#" class="clicker">Categories</a>
      <ul class="menu">
        <li>
          <div class="arrow-up"></div>
          <a href="#">Dashboard</a>
        </li>
        <li><a href="#">Settings</a>
        </li>
        <li><a href="#">Privacy</a>
        </li>
        <li><a href="#">Help</a>
        </li>
        <li><a href="#">Sign out</a>
        </li>
      </ul>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;