feat:添加小地图功能

main
zhu.yawen 1 year ago
parent 9cc6641313
commit ba9718f39f

@ -43,6 +43,7 @@
@scroll="throttleScroll"
>
<div
id="screen-container"
ref="containerRef"
class="screen-container grid-bg"
:style="containerRefStyle"
@ -54,6 +55,31 @@
<slot />
</div>
</div>
<div
id="minimap"
class="minimap"
>
<div class="mapHeader" id="mapHeader">
<div>
<span>小地图</span>
</div>
<div class="showMap" @click="showMinimap">
<i class="el-icon-arrow-down" style="width:20px;height:20px;color:#fff;" v-if="!mapShow"/>
<i class="el-icon-arrow-up" style="width:20px;height:20px;color:#fff;" v-if="mapShow"/>
</div>
</div>
<div
id="selectWin"
class="selectWin"
v-show="mapShow"
>
<div
id="selectionWin"
class="selectionWin"
/>
</div>
<div class="miniView" />
</div>
</div>
</div>
</template>
@ -85,6 +111,10 @@ export default {
},
data () {
return {
mapShow: true, //
canvasLeft: 0, // left
canvasTop: 0, // top
isDrag: false, //
startX: 0,
startY: 0,
lines: {
@ -130,6 +160,16 @@ export default {
if (this.fitZoom === this.zoom) {
this.initZoom()
}
},
mapShow (value) {
const mapElement = document.getElementById('minimap')
const selectElement = document.getElementById('selectWin')
console.log(value, selectElement.getBoundingClientRect().height)
if (!value) {
mapElement.style.bottom = parseFloat(window.getComputedStyle(mapElement).bottom) + 150 + 'px'
} else {
mapElement.style.bottom = parseFloat(window.getComputedStyle(mapElement).bottom) - 150 + 'px'
}
}
},
computed: {
@ -166,16 +206,135 @@ export default {
}
},
mounted () {
// canvasLeft canvasTop
const canvasRect = document.querySelector('#canvas').getBoundingClientRect()
this.canvasLeft = canvasRect.left
this.canvasTop = canvasRect.top
//
this.listenSize()
this.initRuleHeight()
this.throttleScroll()
this.throttleDrag()
},
methods: {
...mapMutations('bigScreen', [
'changeZoom',
'changeFitZoom'
]),
throttleDrag () {
throttle(() => {
this.dragSelection()
this.viewMapDrag()
}, 100)()
},
//
dragSelection () {
const that = this
const draggableElement = document.getElementById('selectionWin')
const dragContainer = document.getElementById('selectWin')
const screenElement = document.getElementById('screens')
const screenContainer = document.getElementById('screen-container')
const maxContainer = document.querySelector('.bs-page-design-wrap')
//
let posX, posY
//
let initialX, initialY
//
let scrollTop, scrollLeft
draggableElement.addEventListener('mousedown', function (event) {
that.isDrag = true
posX = event.clientX
posY = event.clientY
initialX = draggableElement.getBoundingClientRect().left - dragContainer.getBoundingClientRect().left
initialY = draggableElement.getBoundingClientRect().top - dragContainer.getBoundingClientRect().top
scrollLeft = screenElement.scrollLeft
scrollTop = screenElement.scrollTop
maxContainer.addEventListener('mousemove', function (event) {
if (that.isDrag) {
//
let moveX = event.clientX - posX
let moveY = event.clientY - posY
//
if (moveX < -initialX) {
moveX = -initialX
} else if (moveX > dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width - initialX) {
moveX = dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width - initialX
}
if (moveY < -initialY) {
moveY = -initialY
} else if (moveY > dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height - initialY) {
moveY = dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height - initialY
}
const newX = moveX + initialX
const newY = moveY + initialY
//
draggableElement.style.left = newX + 'px'
draggableElement.style.top = newY + 'px'
//
const percentageX = moveX / (dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width)
const percentageY = moveY / (dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height)
//
const scrollTopLength = percentageY * (screenContainer.getBoundingClientRect().height - screenElement.getBoundingClientRect().height)
const scrollLeftLength = percentageX * (screenContainer.getBoundingClientRect().width - screenElement.getBoundingClientRect().width)
screenElement.scrollLeft = scrollLeft + scrollLeftLength
screenElement.scrollTop = scrollTop + scrollTopLength
}
})
})
maxContainer.addEventListener('mouseup', function (event) {
that.isDrag = false
})
draggableElement.addEventListener('mouseup', function () {
that.isDrag = false
})
// H5
draggableElement.ondragstart = function (ev) {
ev.preventDefault()
}
draggableElement.ondragend = function (ev) {
ev.preventDefault()
}
screenElement.ondragstart = function (ev) {
ev.preventDefault()
}
screenElement.ondragend = function (ev) {
ev.preventDefault()
}
},
//
showMinimap () {
this.mapShow = !this.mapShow
},
//
viewMapDrag () {
const mapElement = document.getElementById('minimap')
const mapDragElement = document.getElementById('mapHeader')
const pageElement = document.querySelector('.bs-page-design-wrap')
let mapDrag = false
let curX, curY
let curBottom, curRight
mapDragElement.addEventListener('mousedown', function (event) {
mapDrag = true
curX = event.clientX
curY = event.clientY
curBottom = window.getComputedStyle(mapElement).bottom
curRight = window.getComputedStyle(mapElement).right
pageElement.addEventListener('mousemove', function (event) {
if (mapDrag) {
const dragX = event.clientX - curX
const dragY = event.clientY - curY
mapElement.style.bottom = parseInt(curBottom) - dragY + 'px'
mapElement.style.right = parseInt(curRight) - dragX + 'px'
}
})
})
pageElement.addEventListener('mouseup', function () {
mapDrag = false
})
mapDragElement.addEventListener('mouseup', function () {
mapDrag = false
})
},
listenSize () {
window.onresize = throttle(() => {
this.initRuleHeight()
@ -224,7 +383,9 @@ export default {
const canvasRect = document
.querySelector('#canvas')
.getBoundingClientRect()
const container = document.querySelector('#selectWin').getBoundingClientRect()
const screenContainer = document.querySelector('#screen-container').getBoundingClientRect()
const draggableElement = document.getElementById('selectionWin')
//
const startX = (screensRect.left + this.thick - canvasRect.left) / this.scale
const startY = (screensRect.top + this.thick - canvasRect.top) / this.scale
@ -236,6 +397,16 @@ export default {
x: this.startX * this.scale + 50 - this.thick,
y: this.startY * this.scale + 50 - this.thick
})
//
if (!this.isDrag) {
const leftDrag = canvasRect.left - this.canvasLeft
const topDrag = canvasRect.top - this.canvasTop
//
const leftLength = leftDrag / (screenContainer.width - screensRect.width - 9) * (container.width - draggableElement.getBoundingClientRect().width)
const topLength = topDrag / (screenContainer.height - screensRect.height - 9) * (container.height - draggableElement.getBoundingClientRect().height)
draggableElement.style.left = -leftLength + 'px'
draggableElement.style.top = -topLength + 'px'
}
},
//
initZoom () {
@ -298,6 +469,48 @@ export default {
height: 6000px;
}
.minimap{
position: fixed;
bottom: 15px;
right: 15px;
border: 1px solid #f6f7fb;
z-index:10000;
/*cursor: move;*/
}
.minimap .mapHeader{
background-color:#303640;
padding: 0 10px;
display: flex;
justify-content: space-between;
height: 20px;
width: 150px;
font-size: 12px;
border-bottom: 1px solid #fff;
color: #ffffff;
cursor: pointer;
span {
user-select: none;
}
}
.minimap .selectWin{
background-color: #232832;
height: 150px;
width: 150px;
position: relative;
}
.minimap .selectionWin{
position: absolute;
left: 0px;
top: 0px;
width: 30px;
height: 30px;
background-color: white;
opacity: 0.5;
cursor: move;
}
.scale-value {
position: absolute;
left: 0;

Loading…
Cancel
Save