
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0,
minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script src="https://lib.baomitu.com/vue/2.6.11/vue.common.dev.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
body{
background-color: #ffeee6;
}
.crumbs {
height: 40px;
font-size: 14px;
display: flex;
width: 100%;
white-space: nowrap;
overflow-x: scroll;
background-color: #fff;
}
.crumbs-item {
height: 40px;
line-height: 40px;
box-sizing: border-box;
display: flex;
}
.crumbs-item>span {
padding: 0 10px;
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.crumbs-item:nth-child(1) {
margin-left: 0;
}
.crumbs-item::after {
box-sizing: border-box;
content: "";
display: inline-block;
margin-left: -37px;
border-top: 20px solid transparent;
border-left: 20px solid transparent;
border-right: 0px solid transparent;
border-bottom: 20px solid transparent;
z-index: 1;
}
.theMiddletriangle {
border-top: 20px solid transparent;
border-left: 20px solid #ebebeb;
border-right: 20px solid transparent;
border-bottom: 20px solid transparent;
z-index: 3;
}
.crumbs-item-active {
background-color: #ebebeb;
}
.crumbs-item-active::after {
background-color: #ebebeb;
border-left: 20px solid #fff;
}
.crumbs-item-activeS::after {
background-color: #fff;
}
.theMiddletriangle-active {
visibility: hidden;
}
</style>
</head>
<body>
<div id="box">
<div class="crumbs">
<div class="crumbs-item" v-for="(i,j) in PathAccessHistory" :key="j" @click="onSkip" :data-id='i.id'>
<span>{{i.text}}</span>
<div class="theMiddletriangle"></div>
</div>
</div>
<div style="margin-top: 100px;">
<div>点击单个路径也跳转</div>
<button @click="onAdd">点击添加一个路径</button>
<button @click="onDel">点击删除一个路径</button>
</div>
</div>
</body>
<script>
new Vue({
el: "#box",
name: "1",
data: {
PathAccessHistory: [{id:1,text:'面包屑'}],
},
watch: {
PathAccessHistory() {
this.init()
}
},
methods: {
init() {
let that = this
this.$nextTick(() => {
let crumbsItem_List = document.querySelectorAll('.crumbs-item'),
theMiddletriangle_List = document.querySelectorAll('.theMiddletriangle')
document.querySelector('.crumbs').scrollLeft = document.querySelector('.crumbs').scrollWidth - crumbsItem_List[crumbsItem_List.length-1].offsetWidth
crumbsItem_List.forEach((i,j)=>{
i.onclick = function(){
let index = that.PathAccessHistory.findIndex(item=>item.id==i.getAttribute('data-id'))
if(j==that.PathAccessHistory.length-1) return
that.PathAccessHistory = that.PathAccessHistory.slice(0,index+1)
}
})
for (let i = 0; crumbsItem_List[i]; i++) {
crumbsItem_List[i].classList.remove('crumbs-item-active')
crumbsItem_List[i].classList.remove('crumbs-item-activeS')
theMiddletriangle_List[i].classList.remove('theMiddletriangle-active')
}
if (crumbsItem_List.length == 1) {
theMiddletriangle_List[0].classList.add('theMiddletriangle-active')
return
}
for (let i = 0; crumbsItem_List[i]; i++) {
if (i == crumbsItem_List.length - 1) {
theMiddletriangle_List[i].classList.add('theMiddletriangle-active')
return
}
crumbsItem_List[i].classList.add('crumbs-item-active')
if (i == crumbsItem_List.length - 2) {
crumbsItem_List[i].classList.add('crumbs-item-activeS')
}
}
})
},
onAdd() {
let arr = [{id:0,text:"面包屑00"}, {id:0,text:"阿松大01"}, {id:0,text:"啊实打实的02"}, {id:0,text:"阿德撒旦03"},{id:0,text:"多个地方04"}]
let json = arr[parseInt(Math.random() * (arr.length))]
json['id'] = Math.random() * 100000000
this.PathAccessHistory.push(json)
},
onDel() {
if (this.PathAccessHistory.length == 1) return
this.PathAccessHistory.pop()
},
onSkip(e){
console.log(e.target.innerText)
}
},
mounted() {
this.init()
}
})
</script>
</html>