不管是pc端仍是手機端,都有相似左側分類,點擊後右側切換內容的功能頁面。javascript
要想實現這個功能,首先第一步是要掌握左右佈局的方法。css
推薦使用flex彈性佈局html
.parent { display: flex; } .left { width: 200px; height: 100%; background-color: red; } .right { display: flex; flex: 1; height: 100%; background-color: blue; }
也能夠使用absolute定位,經過left調整位置。vue
<ul id="J_category" class="item"> <li v-for="item in category" @click="clickme(item.id)">{{ item.text }}</li> </ul>
在菜單中添加點擊事件,點擊事件中傳入相關的參數用於獲取右側內容。java
在點擊事件中處理右側的顯示內容,完整代碼以下:npm
<!DOCTYPE html> <head> <title>左側商品分類菜單</title> <script src="https://cdn.jsdelivr.net/npm/vue"></script> </head> <body> <style>html{color:#000;background:#FFF}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,select,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,button,caption,cite,code,dfn,em,input,optgroup,option,select,strong,textarea,th,var{font:inherit}del,ins{text-decoration:none}li{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0;font-variant:normal}sup{vertical-align:baseline}sub{vertical-align:baseline}legend{color:#000} .sub-col{position:relative;z-index:999;} .category{width:230px;border:1px solid #8A0E00;} .category h3 {height:30px;line-height:30px;text-indent:15px;background:#A91319;color:#fff;} .category ul li{height:30px;line-height:30px;text-indent:35px;background:#FFF8F6 url(arrow-r.png) no-repeat 205px center;border-bottom:1px solid #ECECEC;border-top:1px solid #fff;cursor:pointer;color:#A71F37;} .category ul li:hover{background-color:#8A0E00;color:#fff;} .pop-category{border:2px solid #8A0E00;background:#FDF5F5;position:absolute;left:200px;top:40px;z-index:1000;} .pop-category .sub-item{width:390px;height:350px;} </style> <div class="category" id="test"> <h3>全部商品分類</h3> <ul id="J_category" class="item"> <li v-for="item in category" @click="clickme(item.id)">{{ item.text }}</li> </ul> <div id="J_popCategory" class="pop-category"> <div class='sub-item' style='display:none;' id="a">潮流服飾</div> <div class='sub-item' style='display:none;' id="b">精品鞋包</div> <div class='sub-item' style='display:none;' id="c">美容護膚</div> <div class='sub-item' style='display:none;' id="d">珠寶飾品</div> <div class='sub-item' style='display:none;' id="e">運動戶外</div> <div class='sub-item' style='display:none;' id="f">手機數碼</div> <div class='sub-item' style='display:none;' id="g">居家生活</div> <div class='sub-item' style='display:none;' id="h">家電家裝</div> <div class='sub-item' style='display:none;' id="i">母嬰用品</div> <div class='sub-item' style='display:none;' id="j">食品保健</div> </div> </div> <script> new Vue({ el: '#test', data: { category: [{ text: "潮流服飾", id: "a" }, { text: "精品鞋包", id: "b" }, { text: "美容護膚", id: "c" }, { text: "珠寶飾品", id: "d" }, { text: "運動戶外", id: "e" }, { text: "手機數碼", id: "f" }, { text: "居家生活", id: "g" }, { text: "家電家裝", id: "h" }, { text: "母嬰用品", id: "i" }, { text: "食品保健", id: "j" }] }, mounted: function () { this.init(); }, methods: { init() { // TODO 初始化數據 }, clickme(id) { var subItems = document.getElementsByClassName('sub-item', 'div'); for (var j = 0; j < subItems.length; j++) { subItems[j].style.display = 'none'; } const ele = document.getElementById(id) console.log(id, ele) ele.style.display = 'block'; } } }) </script> </body> </html>
轉評贊就是最大的鼓勵
佈局