如何使标签下拉箭头可点击

时间:2018-04-19 03:26:47

标签: html css navigation hover clickable

我创建了一个垂直导航手风琴菜单,您可以在其中悬停并单击任何标题(标签)以打开子菜单。

由于某种原因,我的下拉箭头无法点击,但标签的其余部分是可点击的并且功能正常。

如何使我的“箭头”可点击并像标签的其余部分一样运作?

Here's the jsfiddle

CSS:

/*Global*/

html,
body {
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

/*Functionalty*/

.multi-level,
.item ul,
.nav input[type="checkbox"] {
  display: none;
}

#menu:checked~.multi-level,
.item input:checked~ul {
  display: block;
}

/*Arrow*/

.arrow {
  width: 10px;
  height: 10px;
  vertical-align: middle;
  float: left;
  margin: 9px 0.5em 0 2em;
}

.item input+.arrow {
  transform: rotate(-90deg);
  transition: 0.1s;
}

.item input:checked+.arrow {
  transform: rotate(0deg);
  transition: 0.1s;
}

img {
  z-index: 1;
  position: relative;
}

/*Styles*/

label:hover {
  cursor: pointer;
  background-color: #f2f2f2;
  position: relative;
}

li:hover {
  background-color: #f2f2f2;
}

label {
  width: 100%;
  display: block;
}

.nav {
  width: 100%;
  background-color: white;
  overflow-x: hidden;
  /*border-bottom: 1px solid #CFD8DC;*/
}

/*#nav-icon {
    font-size: 28px;
    line-height: 50px;
    padding-left: 1em;
    color: white;
    background-color: #F44336;
}*/

.nav ul,
.nav li,
label {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
  width: 100%;
}

.item ul {
  padding: 0 0.25em;
}

.nav li a {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
}

.item {
  margin: 0 0 0 -1.5em;
}

.sub-item {
  padding: 0 0m;
}

HTML:

<div class="nav">
  <input type="checkbox" id="menu" checked />
  <label for="menu"></label>

  <div class="multi-level">
    <div class="item">
      <input type="checkbox" id="A" />
      <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="A">Category One</label>
      <ul>
        <li><a href="#">Sub-Category One</a></li>
        <li><a href="#">Sub-Category Two</a></li>
        <li><a href="#">Sub-Category Three</a></li>
      </ul>
    </div>
    <div class="item">
      <input type="checkbox" id="B" />
      <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="B">Category Two</label>
      <ul>
        <li><a href="#">Sub-Category One</a></li>
        <li>
          <div class="sub-item">
            <input type="checkbox" id="B-A" />
            <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="B-A">Sub-Category Two</label>

            <ul>
              <li><a href="#">Sub-Sub-Category-1</a></li>
              <li><a href="#">Sub-Sub-Category-2</a></li>
              <li><a href="#">Sub-Sub-Category-3</a></li>
              <li><a href="#">Sub-Sub-Category-4</a></li>
              <li><a href="#">Sub-Sub-Category-5</a></li>
              <li><a href="#">Sub-Sub-Category-6</a></li>
            </ul>
          </div>
        </li>
      </ul>
    </div>
    <div class="item">
      <input type="checkbox" id="C" />
      <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="C">Category Three</label>
      <ul>
        <li><a href="#">Sub-Category One</a></li>
        <li><a href="#">Sub-Category Two</a></li>
        <li><a href="#">Sub-Category Three</a></li>
      </ul>
    </div>
    <div class="item">
      <input type="checkbox" id="D" />
      <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="D">Category Four</label>
      <ul>
        <li><a href="#">Sub-Category One</a></li>
        <li><a href="#">Sub-Category Two</a></li>
        <li><a href="#">Sub-Category Three</a></li>
      </ul>
    </div>

  </div>

</div>

3 个答案:

答案 0 :(得分:1)

您可以使用:before替换img代码,以确保点击label代替img

/*Global*/

html,
body {
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

/*Functionalty*/

.multi-level,
.item ul,
.nav input[type="checkbox"] {
  display: none;
}

#menu:checked~.multi-level,
.item input:checked~ul {
  display: block;
}

/*Arrow*/

.arrow {
  width: 10px;
  height: 10px;
  vertical-align: middle;
  float: left;
  margin: 9px 0.5em 0 2em;
}

/*Styles*/

label:hover {
  cursor: pointer;
  background-color: #f2f2f2;
  position: relative;
}

li:hover {
  background-color: #f2f2f2;
}

label {
  width: 100%;
  display: block;
  position: relative;
}

.nav {
  width: 100%;
  background-color: white;
  overflow-x: hidden;
  /*border-bottom: 1px solid #CFD8DC;*/
}

/*#nav-icon {
    font-size: 28px;
    line-height: 50px;
    padding-left: 1em;
    color: white;
    background-color: #F44336;
}*/

.nav ul,
.nav li,
label {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
  width: 100%;
}

.item ul {
  padding: 0 0.25em;
}

.nav li a {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
}

.item {
  margin: 0 0 0 -1.5em;
}

.item {
  
  margin-left: 0;
}
.item label:before {
  content: '';
  background: url(https://png.icons8.com/material/50/000000/expand-arrow.png);
  width: 10px;
  background-size: 100%;
  height: 10px;
  position: absolute;
  top: 9px;
  left: 13px;
  transform: rotate(-90deg);
}

.item input:checked +label:before {
  transform: rotate(0);
}

.sub-item {
  padding: 0 0m;
}
<div class="nav">
	<input type="checkbox" id="menu" checked />
	<label for="menu"></label>
  
	<div class="multi-level">
	  <div class="item">
		<input type="checkbox" id="A" />
		<label for="A">Category One</label>
		<ul>
		  <li><a href="#">Sub-Category One</a></li>
		  <li><a href="#">Sub-Category Two</a></li>
		  <li><a href="#">Sub-Category Three</a></li>
		</ul>
	  </div>
	  <div class="item">
		<input type="checkbox" id="B" />
		<label for="B">Category Two</label>
		<ul>
		  <li><a href="#">Sub-Category One</a></li>
		  <li>
			<div class="sub-item">
			  <input type="checkbox" id="B-A" />
			  <label for="B-A">Sub-Category Two</label>
  
			  <ul>
				<li><a href="#">Sub-Sub-Category-1</a></li>
				<li><a href="#">Sub-Sub-Category-2</a></li>
				<li><a href="#">Sub-Sub-Category-3</a></li>
				<li><a href="#">Sub-Sub-Category-4</a></li>
				<li><a href="#">Sub-Sub-Category-5</a></li>
				<li><a href="#">Sub-Sub-Category-6</a></li>
			  </ul>
			</div>
		  </li>
		</ul>
	  </div>
	  <div class="item">
		<input type="checkbox" id="C" />
		<label for="C">Category Three</label>
		<ul>
		  <li><a href="#">Sub-Category One</a></li>
		  <li><a href="#">Sub-Category Two</a></li>
		  <li><a href="#">Sub-Category Three</a></li>
		</ul>
	  </div>
	  <div class="item">
		<input type="checkbox" id="D" />
		<label for="D">Category Four</label>
		<ul>
		  <li><a href="#">Sub-Category One</a></li>
		  <li><a href="#">Sub-Category Two</a></li>
		  <li><a href="#">Sub-Category Three</a></li>
		</ul>
	  </div>
  
	</div>
  
  </div>
  

https://jsfiddle.net/4v843n7f/34/

答案 1 :(得分:0)

我可能会建议在标签元素上使用:before选择器(而不是在每个标签之前插入img)。这会解决您的问题,箭头不可点击并会使HTML更干燥

    <div class="nav">
 <input type="checkbox" id="menu" checked />
 <label for="menu"></label>

 <div class="multi-level">
   <div class="item">
  <input type="checkbox" id="A" />
  <label for="A">Category One</label>
  <ul>
    <li><a href="#">Sub-Category One</a></li>
    <li><a href="#">Sub-Category Two</a></li>
    <li><a href="#">Sub-Category Three</a></li>
  </ul>
</div>
<div class="item">
  <input type="checkbox" id="B" />
  <label for="B">Category Two</label>
  <ul>
    <li><a href="#">Sub-Category One</a></li>
    <li>
      <div class="sub-item">
        <input type="checkbox" id="B-A" />
        <label for="B-A">Sub-Category Two</label>

        <ul>
          <li><a href="#">Sub-Sub-Category-1</a></li>
          <li><a href="#">Sub-Sub-Category-2</a></li>
          <li><a href="#">Sub-Sub-Category-3</a></li>
          <li><a href="#">Sub-Sub-Category-4</a></li>
          <li><a href="#">Sub-Sub-Category-5</a></li>
          <li><a href="#">Sub-Sub-Category-6</a></li>
        </ul>
      </div>
    </li>
  </ul>
</div>
<div class="item">
  <input type="checkbox" id="C" />
  <label for="C">Category Three</label>
  <ul>
    <li><a href="#">Sub-Category One</a></li>
    <li><a href="#">Sub-Category Two</a></li>
    <li><a href="#">Sub-Category Three</a></li>
  </ul>
</div>
<div class="item">
  <input type="checkbox" id="D" />
  <label for="D">Category Four</label>
  <ul>
    <li><a href="#">Sub-Category One</a></li>
    <li><a href="#">Sub-Category Two</a></li>
    <li><a href="#">Sub-Category Three</a></li>
  </ul>
</div>

.item label {
  position: relative;
  width: 100%;
  display: block;
  padding-left: 2em;
}

.item label:before {
  position: absolute;
  content:'';
  left: 0;
  top: 0;
  width: 2em;
  height: 2em;
  background-image: url(https://png.icons8.com/material/50/000000/expand-arrow.png);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 10px;
  transform: rotate(-90deg);
  transition: 0.1s;
}

label:hover {
  cursor: pointer;
  background-color: #f2f2f2;
  position: relative;
}
.item input:checked+label:before {
transform: rotate(0deg);
  transition: 0.1s;

这是一个小提琴https://jsfiddle.net/Jimujing/3b95ubsx/

答案 2 :(得分:0)

尝试在<label>标记内设置图片。为此你必须改变一些css和html代码。请检查以下内容。 CSS

html,
body {
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

/*Functionalty*/

.multi-level,
.item ul,
.nav input[type="checkbox"] {
  display: none;
}

#menu:checked~.multi-level,
.item input:checked~ul {
  display: block;
}

/*Arrow*/

.arrow {
  width: 10px;
  height: 10px;
  vertical-align: middle;
  float: left;
  margin: 9px 0.5em 0 2em;
}

.item .arrow {
  transform: rotate(-90deg);
  transition: 0.1s;
}

.item input:checked+.arrow {
  transform: rotate(0deg);
  transition: 0.1s;
}

img {
  z-index: 1;
  position: relative;
}

/*Styles*/

label:hover {
  cursor: pointer;
  background-color: #f2f2f2;
  position: relative;
}

li:hover {
  background-color: #f2f2f2;
}

label {
  width: 100%;
  display: block;
}

.nav {
  width: 100%;
  background-color: white;
  overflow-x: hidden;
  /*border-bottom: 1px solid #CFD8DC;*/
}

...

.nav ul,
.nav li,
label {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
  width: 100%;
}
label {
    padding: 0;
}
.item ul {
  padding: 0 0.25em;
}

.nav li a {
  line-height: 30px;
  margin: 0;
  padding: 0 2em;
  list-style: none;
  text-decoration: none;
  color: #000;
  font-weight: 100;
}

.item {
  margin: 0 0 0 -1.5em;
}

.sub-item {
  padding: 0 0m;
}

HTML

<div class="nav">
    <input type="checkbox" id="menu" checked />
    <label for="menu"></label>
    <div class="multi-level">
        <div class="item">
            <input type="checkbox" id="A" />
            <label for="A"> <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"> Category One</label>
            <ul>
                <li><a href="#">Sub-Category One</a></li>
                <li><a href="#">Sub-Category Two</a></li>
                <li><a href="#">Sub-Category Three</a></li>
            </ul>
        </div>
        <div class="item">
            <input type="checkbox" id="B" />
            <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="B">Category Two</label>
            <ul>
                <li><a href="#">Sub-Category One</a></li>
                <li>
                    <div class="sub-item">
                        <input type="checkbox" id="B-A" />
                        <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="B-A">Sub-Category Two</label>
                        <ul>
                            <li><a href="#">Sub-Sub-Category-1</a></li>
                            <li><a href="#">Sub-Sub-Category-2</a></li>
                            <li><a href="#">Sub-Sub-Category-3</a></li>
                            <li><a href="#">Sub-Sub-Category-4</a></li>
                            <li><a href="#">Sub-Sub-Category-5</a></li>
                            <li><a href="#">Sub-Sub-Category-6</a></li>
                        </ul>
                    </div>
                </li>
            </ul>
        </div>
        <div class="item">
            <input type="checkbox" id="C" />
            <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="C">Category Three</label>
            <ul>
                <li><a href="#">Sub-Category One</a></li>
                <li><a href="#">Sub-Category Two</a></li>
                <li><a href="#">Sub-Category Three</a></li>
            </ul>
        </div>
        <div class="item">
            <input type="checkbox" id="D" />
            <img src="https://png.icons8.com/material/50/000000/expand-arrow.png" class="arrow"><label for="D">Category Four</label>
            <ul>
                <li><a href="#">Sub-Category One</a></li>
                <li><a href="#">Sub-Category Two</a></li>
                <li><a href="#">Sub-Category Three</a></li>
            </ul>
        </div>
    </div>
</div>