如何仅显示父级和子级元素? Java脚本

时间:2019-07-29 12:33:08

标签: javascript html css drop-down-menu

我希望当我单击父元素时,可以向我显示他的子元素。

由于存在多种嵌套,

  • 当我选择“列表子2”时,我希望“列表1”消失。
  • 当我再次单击“列表1”时,我想看到“列表1”和“列表2”子菜单。
  • 单击“列表子2”以显示“列表3子菜单”,然后单击“列表1”。

另一件事,如果列表是左侧的打开视图箭头,而显示器没有右侧打开,则下拉箭头应该是。

高优先级是如何显示属性的父链接和子链接。

Codepen代码: https://codepen.io/anon/pen/RXKBeq

HTML:

$facebookBlue: #153161;
$strongRed: #9a0000;
$strongYellow: #ee8f01;
$white: #ffffff;
 * {
    box-sizing: border-box;
}
.dropdown {
    position: relative;
    display: inline-block;
    .dropbtn {
        background-color: $facebookBlue;
        color: $white;
        font-size: 17px;
        font-weight: 600;
        border: none;
        cursor: pointer;
        height: 55px;
        background: #153161;
        border-bottom-left-radius: 7px;
        border-bottom-right-radius: 7px;
        padding: 12px 50px;
        position: relative;
        width: 360px;
        text-align: left;
        i {
            margin-left: 30px;
            color: #8391ab;
            position: absolute;
            top: 50%;
            right: 25px;
            transform: translateY(-50%);
        }
        .arrow {
            width: 0;
            height: 0;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            border-top: 10px solid #8191aa;
            margin: 100%;
            padding-top: 4px;
            z-index: 999;
        }
    }
    .dropbtn-two {
        background: $strongRed;
    }
    .dropbtn-three {
        background: $strongYellow;
    }
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    width: 330px;
    z-index: 999;
    a {
        color: black;
        padding: 12px 25px;
        text-decoration: none;
        display: flex;
        justify-content: flex-start;
        width: 100%;
        &:hover {
            background-color: #F8F8F8
        }
    }
    .rightt {
        display: inline-block;
        cursor: pointer;
        position: absolute;
        right: 20px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 999;
        &:after {
            content: '';
            display: inline-block;
            width: 9px;
            height: 9px;
            border-top: 0.2em solid #ababab;
            border-right: 0.2em solid #ababab;
            -moz-transform: rotate(45deg);
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
        }
    }
    .left {
        display: inline-block;
        cursor: pointer;
        position: absolute;
        right: 20px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 999;
        &:after {
            content: '';
            display: inline-block;
            width: 29px;
            height: 29px;
            border-top: 0.2em solid #ababab;
            border-right: 0.2em solid #ababab;
            transform: rotate(45deg);
        }
    }
    .item-has-children {
        a {
            position: relative;
        }
    }
}

.dropdown:hover .dropdown-content {
    display: block;
    background: white;
    opacity: 0.95;
    width: 100%;
}

.hr2 {
    height: 1px;
    background: #ccc;
    border-bottom: 1px solid #fff;
    border-top: 1px solid #ccc;
}

.sub-menu {
    display: none;
}

.sub-menu.selected {
    display: flex;
    flex-direction: column;
    transition: transform 0.6s;
}

ul {
    list-style: none;
    padding: 0 0px;
    width: 100%;
    height: 100%;
}

.rightt.selected {
    left: 40px;
    top: 25%;
    right: auto;
    transform: translateY(0);
    transform: rotate(180deg);
}

.padd-left {
    padding-left: 0px;
}

.padd-left.selected {
    padding-left: 70px;
}

CSS(无礼)

const links = document.querySelectorAll(".item-has-children");
const padd_left = document.querySelectorAll(".padd-left"); 
links.forEach((link) => {
  link.addEventListener('click', (e) => { 
    const index = Array.from(links).indexOf(link)
    if (e.target.nextElementSibling.classList.contains('selected')) {
      e.target.nextElementSibling.classList.remove('selected') 
      e.target.querySelector('.rightt').classList.remove('selected');
      Array.from(padd_left)[index].classList.remove('selected')
    } else {
      e.target.nextElementSibling.classList.add('selected')
      e.target.querySelector('.rightt').classList.add('selected');
      Array.from(padd_left)[index].classList.add('selected')
    }
  })
})

JavaScript:

<fragment
        class = "com.epicodus.talkaboutit.ui.PostListFragment"
        android:id="@+id/postListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_post_list"
        android:layout_below="@+id/categoryNameTextView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="69dp"
        android:layout_above="@+id/addPostButton" />

2 个答案:

答案 0 :(得分:0)

bootstrap的早期版本中有一些内容不是最新的。

还要检出this。一些家伙已经做到了,但是在b4中。

答案 1 :(得分:0)

这是一个简单的解决方案,但使用了jQuery。请查看分叉的CodePen:https://codepen.io/lakialex/pen/eqvEBR

(local)

我稍微改变了Sass。 例如,不需要.padd-left类,因为您可以为.selected类设置样式以用于左填充和箭头更改。

const links = $('.item-has-children a');

links.each(function() {
    $(this).on('click', function() {
      if ($(this).hasClass('selected')) {
        $(this).parent().parent().prev().show();
        $(this).next('ul').hide();
        $(this).removeClass('selected');
      } else {
        $(this).parent().parent().prev().hide();
        $(this).next('ul').show();
        $(this).addClass('selected');
      }
    });
});

请记住,我已经在CodePen设置中添加了jQuery。

干杯!


更新

CodePen已使用与以前的jQuery代码完全一样的香草JS更新。

$facebookBlue: #153161;
$strongRed: #9a0000;
$strongYellow: #000;
$white: #ffffff;
 * {
    box-sizing: border-box;
}
body {
  display: flex;
  padding-top: 40px;
}
.dropdown {
    position: relative;
    display: inline-block;
  margin: 0 auto;
    .dropbtn {
        background-color: $facebookBlue;
        color: $white;
        font-size: 17px;
        font-weight: 600;
        border: none;
        cursor: pointer;
        height: 55px;
        background: #153161;
        border-bottom-left-radius: 7px;
        border-bottom-right-radius: 7px;
        padding: 12px 50px;
        position: relative;
        width: 360px;
        text-align: left;
        i {
            margin-left: 30px;
            color: #8391ab;
            // opacity: 0.2;
            position: absolute;
            top: 50%;
            right: 25px;
            transform: translateY(-50%);
        }
        .arrow {
            width: 0;
            height: 0;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            border-top: 10px solid #8191aa;
            margin: 100%;
            padding-top: 4px;
            z-index: 999;
        }
    }
    .dropbtn-two {
        background: $strongRed;
    }
    .dropbtn-three {
        background: $strongYellow;
    }
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    width: 330px;
    z-index: 999;
    a {
        color: black;
        padding: 12px 25px;
        text-decoration: none;
        display: flex;
        justify-content: flex-start;
        width: 100%;
        &:hover {
            background-color: #F8F8F8
        }
    .rightt {
      display: inline-block;
      // width: 9px;
      // height: 9px;
      cursor: pointer;
      // padding-left: 180px;
      position: absolute;
      right: 20px;
      top: 50%;
      transform: translateY(-50%);
      z-index: 999;
      &:after {
        content: '';
        display: inline-block;
        width: 9px;
        height: 9px;
        border-top: 0.2em solid #ababab;
        border-right: 0.2em solid #ababab;
        -moz-transform: rotate(45deg);
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
      }
    }
    &.selected {
      padding-left: 70px;
      .rightt {
        left: 40px;
        top: 25%;
        right: auto;
        transform: translateY(0);
        transform: rotate(180deg);
      }
    }
    }

    .left {
        display: inline-block;
        cursor: pointer;
        position: absolute;
        right: 20px;
        top: 50%;
        transform: translateY(-50%);
        z-index: 999;
        &:after {
            content: '';
            display: inline-block;
            width: 29px;
            height: 29px;
            border-top: 0.2em solid #ababab;
            border-right: 0.2em solid #ababab;
            -moz-transform: rotate(45deg);
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
        }
    }
    .item-has-children {
        a {
            position: relative;
        }
    }
}

.dropdown:hover .dropdown-content {
    display: block;
    background: white;
    opacity: 0.95;
    width: 100%;
}

.hr2 {
    height: 1px;
    background: #ccc;
    border-bottom: 1px solid #fff;
    border-top: 1px solid #ccc;
}

.sub-menu {
    display: none;
}

.sub-menu.selected {
    display: flex;
    flex-direction: column;
    transition: transform 0.6s;
}

ul {
    list-style: none;
    padding: 0 0px;
    width: 100%;
    height: 100%;
}

干杯!