亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JS如何隱藏頁面上的所有類別

JS如何隱藏頁面上的所有類別

蠱毒傳說 2021-05-01 10:07:56
我在網頁上使用類別,我建立了下一個視圖來顯示類別。我創建了一個解決方案,如何對應用程序中的所有數據進行分類。但是我有一些我無法解決的問題。所以問題是,當我第一次打開我的網頁時,列表中的所有類別都是可見的,而我希望類別僅在類別項按下后才需要可見。我的代碼:$(document).ready(function () {    $('.category_list .category_item[category="all"]').addClass('ct_item-active');    $('.category_item').click(function () {        var catProduct = $(this).attr('category');        console.log(catProduct);        $('.category_item').removeClass('ct_item-active');        $(this).addClass('ct_item-active');        $('.product-item').css('transform', 'scale(0)');        function hideProduct() {            $('.product-item').hide();        } setTimeout(hideProduct, 400);        function showProduct() {            $('.product-item[category="' + catProduct + '"]').show();            $('.product-item[category="' + catProduct + '"]').css('transform', 'scale(1)');        } setTimeout(showProduct, 400);    });});.wrap {    max-width: 1100px;    width: 90%;    margin: auto;}.wrap > h1 {    color: #494B4D;    font-weight: 400;    display: flex;    flex-direction: column;    text-align: center;    margin: 15px 0px;}.wrap > h1:after {    content: '';    width: 100%;    height: 1px;    background: #C7C7C7;    margin: 20px 0;}.store-wrapper {    display: flex;    flex-wrap: wrap;}.category_list {    display: flex;    flex-direction: column;    width: 30%;}.category_list .category_item {    display: block;    width: 100%;    padding: 15px 0;    margin-bottom: 20px;    background: #E84C3D;    text-align: center;    text-decoration: none;    color: #fff;}.category_list .ct_item-active {    background: #2D3E50;}.products-list {    width: 70%;    display: flex;    flex-wrap: wrap;}.products-list .product-item {    width: 35%;    margin-left: 3%;    margin-bottom: 25px;    box-shadow: 0px 0px 6px 0px rgba(0,0,0,0.22);    display: flex;    flex-direction: column;    align-items: center;    align-self: flex-start;    transition: all .4s;}因此,如您所見,我沒有在首頁顯示所有類別的func。但是,當我重新加載頁面時,所有類別均可見,此時我不是3,但是我有3個以上,并且我想在按類別項目后顯示此類別。
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

您只需要添加.products-list .product-item { display:none}到CSS。


$(document).ready(function () {

    $('.category_list .category_item[category="all"]').addClass('ct_item-active');


    $('.category_item').click(function () {

        var catProduct = $(this).attr('category');

        console.log(catProduct);


        $('.category_item').removeClass('ct_item-active');

        $(this).addClass('ct_item-active');


        $('.product-item').css('transform', 'scale(0)');

        function hideProduct() {

            $('.product-item').hide();

        } setTimeout(hideProduct, 400);


        function showProduct() {

            $('.product-item[category="' + catProduct + '"]').show();

            $('.product-item[category="' + catProduct + '"]').css('transform', 'scale(1)');

        } setTimeout(showProduct, 400);

    });


});

.wrap {

    max-width: 1100px;

    width: 90%;

    margin: auto;

}


.wrap > h1 {

    color: #494B4D;

    font-weight: 400;

    display: flex;

    flex-direction: column;

    text-align: center;

    margin: 15px 0px;

}


.wrap > h1:after {

    content: '';

    width: 100%;

    height: 1px;

    background: #C7C7C7;

    margin: 20px 0;

}


.store-wrapper {

    display: flex;

    flex-wrap: wrap;

}


.category_list {

    display: flex;

    flex-direction: column;

    width: 30%;

}


.category_list .category_item {

    display: block;

    width: 100%;

    padding: 15px 0;

    margin-bottom: 20px;

    background: #E84C3D;

    text-align: center;

    text-decoration: none;

    color: #fff;

}


.category_list .ct_item-active {

    background: #2D3E50;

}


.products-list {

    width: 70%;

    display: flex;

    flex-wrap: wrap;

}



.products-list .product-item {

    width: 35%;

    margin-left: 3%;

    margin-bottom: 25px;

    box-shadow: 0px 0px 6px 0px rgba(0,0,0,0.22);

    display: flex;

    flex-direction: column;

    align-items: center;

    align-self: flex-start;

    transition: all .4s;

    display:none;

}


.products-list .product-item img {

    width: 100%;

}


.products-list .product-item a {

    display: block;

    width: 100%;

    padding: 8px 0;

    background: #2D3E50;

    color: #fff;

    text-align: center;

    text-decoration: none;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wrap">

    <div class="store-wrapper">

        <div class="category_list">

            <a href="#" class="category_item" category="Technical">Technical</a>

            <a href="#" class="category_item" category="Organizational">Organizational</a>


        </div>

        <section class="products-list">

            <div class="product-item" category="Technical">

                <img src="~/images/100004-200.png" alt="">

                <a href="#">Equipment</a>

            </div>

            <div class="product-item" category="Technical">

                <img src="~/images/100004-200.png" alt="">

                <a href="#">Tool</a>

            </div>

            <div class="product-item" category="Organizational">

                <img src="~/images/100004-200.png" alt="">

                <a href="#">Material</a>

            </div>

        </section>

    </div>

</div>


查看完整回答
反對 回復 2021-05-06
  • 1 回答
  • 0 關注
  • 287 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號