const allBorder = [ 'border-top', 'border-bottom', 'border-left', 'border-right', ] const width = position.find((item) => item.trim().includes('border-width')) const widthValue = width.split(':')?.[1] const styles = position.find((item) => item.trim().includes('border-style')) const stylesValue = styles.split(':')?.[1] const borderValue = `${widthValue} ${value.trim()} ${stylesValue}` allBorder.forEach((item) => { this.sheet.current.setStyle(target, item, borderValue); })优化代码
时间: 2024-02-14 16:28:06 浏览: 29
你可以尝试使用数组的`map`方法来优化这段代码。这样你就不需要使用`forEach`循环来遍历`allBorder`数组了。以下是优化后的代码示例:
```javascript
const allBorder = [
'border-top',
'border-bottom',
'border-left',
'border-right',
];
const width = position.find((item) => item.trim().includes('border-width'));
const widthValue = width.split(':')?.[1];
const styles = position.find((item) => item.trim().includes('border-style'));
const stylesValue = styles.split(':')?.[1];
const borderValue = `${widthValue} ${value.trim()} ${stylesValue}`;
allBorder.map(item => this.sheet.current.setStyle(target, item, borderValue));
```
使用`map`方法可以更简洁地实现对数组元素的遍历,并且可以直接调用`setStyle`方法进行样式设置。
相关问题
请根据代码片段仿写实现div左下角拖拽移动用具体代码实现,import Vue from 'vue' Vue.directive('dialogZoomOut', { bind(el, binding, vnode, oldVnode) { let minWidth = 400;let minHeight = 300;let isFullScreen = false; let nowWidth = 0;let nowHight = 0;let nowMarginTop = 0;const dialogHeaderEl = el.querySelector('.el-dialog__header');const dragDom = el.querySelector('.el-dialog');dragDom.style.overflow = "auto";dialogHeaderEl.onselectstart = new Function("return false");dialogHeaderEl.style.cursor = 'move';const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);let moveDown = (e) => {const disX = e.clientX - dialogHeaderEl.offsetLeft;const disY = e.clientY - dialogHeaderEl.offsetTop;let styL, styT;if (sty.left.includes('%')) {styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);} else {styL = +sty.left.replace(/px/g, '');styT = +sty.top.replace(/px/g, '');};document.onmousemove = function (e) {const l = e.clientX - disX;const t = e.clientY - disY;dragDom.style.left = `${l + styL}px`;dragDom.style.top = `${t + styT}px`;};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}dialogHeaderEl.onmousedown = moveDown;dialogHeaderEl.ondblclick = (e) => {if (isFullScreen == false) {nowHight = dragDom.clientHeight;nowWidth = dragDom.clientWidth;nowMarginTop = dragDom.style.marginTop;dragDom.style.left = 0;dragDom.style.top = 0;dragDom.style.height = "100VH";dragDom.style.width = "100VW";dragDom.style.marginTop = 0;isFullScreen = true;dialogHeaderEl.style.cursor = 'initial';dialogHeaderEl.onmousedown = null;} else {dragDom.style.height = "auto";dragDom.style.width = nowWidth + 'px';dragDom.style.marginTop = nowMarginTop;isFullScreen = false;dialogHeaderEl.style.cursor = 'move';dialogHeaderEl.onmousedown = moveDown;}}let resizeEl = document.createElement("div");dragDom.appendChild(resizeEl);resizeEl.style.cursor = 'se-resize';resizeEl.style.position = 'absolute';resizeEl.style.height = '10px';resizeEl.style.width = '10px';resizeEl.style.right = '0px';resizeEl.style.bottom = '0px';resizeEl.style.zIndex = '99';resizeEl.onmousedown = (e) => {let clientX = e.clientX;let disX = e.clientX - resizeEl.offsetLeft;let disY = e.clientY - resizeEl.offsetTop;document.onmousemove = function (e) {e.preventDefault(); let x = e.clientX - disX + (e.clientX - clientX);let y = e.clientY - disY;dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}}})
以下是仿写的代码实现:
```html
<template>
<div class="dialog-wrapper" v-dialog-zoom-out>
<div class="el-dialog">
<div class="el-dialog__header">Dialog Title</div>
<div class="el-dialog__body">Dialog Content</div>
<div class="el-dialog__footer">
<button @click="$emit('close')">Close</button>
</div>
<div class="el-dialog__resize"></div>
</div>
</div>
</template>
<script>
export default {
name: "Dialog",
};
</script>
<style>
.dialog-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.el-dialog {
position: absolute;
top: 0;
left: 0;
overflow: auto;
max-height: 80vh;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.el-dialog__header {
padding: 16px;
background-color: #f5f7fa;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
cursor: move;
}
.el-dialog__body {
padding: 16px;
}
.el-dialog__footer {
display: flex;
justify-content: flex-end;
padding: 16px;
background-color: #f5f7fa;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
.el-dialog__footer button {
padding: 8px 16px;
background-color: #fff;
border: 1px solid #c1c1c1;
border-radius: 4px;
cursor: pointer;
}
.el-dialog__resize {
position: absolute;
bottom: 0;
right: 0;
width: 10px;
height: 10px;
cursor: se-resize;
background-color: #c1c1c1;
}
</style>
```
```javascript
import Vue from "vue";
Vue.directive("dialogZoomOut", {
bind(el, binding, vnode, oldVnode) {
let minWidth = 400;
let minHeight = 300;
let isFullScreen = false;
let nowWidth = 0;
let nowHight = 0;
let nowMarginTop = 0;
const dialogHeaderEl = el.querySelector(".el-dialog__header");
const dragDom = el.querySelector(".el-dialog");
dragDom.style.overflow = "auto";
dialogHeaderEl.onselectstart = new Function("return false");
dialogHeaderEl.style.cursor = "move";
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
let moveDown = (e) => {
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.offsetTop;
let styL, styT;
if (sty.left.includes("%")) {
styL = +document.body.clientWidth * (+sty.left.replace(/%/g, "") / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/%/g, "") / 100);
} else {
styL = +sty.left.replace(/px/g, "");
styT = +sty.top.replace(/px/g, "");
}
document.onmousemove = function (e) {
const l = e.clientX - disX;
const t = e.clientY - disY;
dragDom.style.left = `${l + styL}px`;
dragDom.style.top = `${t + styT}px`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
dialogHeaderEl.onmousedown = moveDown;
dialogHeaderEl.ondblclick = (e) => {
if (isFullScreen == false) {
nowHight = dragDom.clientHeight;
nowWidth = dragDom.clientWidth;
nowMarginTop = dragDom.style.marginTop;
dragDom.style.left = 0;
dragDom.style.top = 0;
dragDom.style.height = "100VH";
dragDom.style.width = "100VW";
dragDom.style.marginTop = 0;
isFullScreen = true;
dialogHeaderEl.style.cursor = "initial";
dialogHeaderEl.onmousedown = null;
} else {
dragDom.style.height = "auto";
dragDom.style.width = nowWidth + "px";
dragDom.style.marginTop = nowMarginTop;
isFullScreen = false;
dialogHeaderEl.style.cursor = "move";
dialogHeaderEl.onmousedown = moveDown;
}
};
let resizeEl = document.createElement("div");
dragDom.appendChild(resizeEl);
resizeEl.style.cursor = "se-resize";
resizeEl.style.position = "absolute";
resizeEl.style.height = "10px";
resizeEl.style.width = "10px";
resizeEl.style.right = "0px";
resizeEl.style.bottom = "0px";
resizeEl.style.zIndex = "99";
resizeEl.onmousedown = (e) => {
let clientX = e.clientX;
let disX = e.clientX - resizeEl.offsetLeft;
let disY = e.clientY - resizeEl.offsetTop;
document.onmousemove = function (e) {
e.preventDefault();
let x = e.clientX - disX + (e.clientX - clientX);
let y = e.clientY - disY;
dragDom.style.width = x > minWidth ? `${x}px` : minWidth + "px";
dragDom.style.height = y > minHeight ? `${y}px` : minHeight + "px";
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
},
});
```
上述代码实现了一个类似于Element UI中的Dialog组件的拖拽和缩放功能。在使用时,只需要将 `v-dialog-zoom-out` 指令绑定到Dialog组件的容器元素上,即可实现拖拽和缩放功能。
<script setup> import useGetUrl from "@/hooks/useGetUrl"; import { reactive ,onMounted} from "vue"; const state = reactive({ inputText: "", isKeyboardVisible: false, messages: [ { content: "施主你好,很高兴为您服务!", msg: { time:"" } }, ], sendMessage: () => { const content = state.inputText.trim(); if (content) { const time = new Date(); const message = { id: state.messages.length + 1, content, time, isUser: true, }; state.messages.push(message); state.inputText = ""; state.replyMessage(); } }, replyMessage: () => { const content = "尊敬的施主,您好。我们已收到您的咨询,请稍后。"; const time = new Date(); const message = { id: state.messages.length + 1, content, time, isUser: false, }; setTimeout(() => { state.messages.push(message); }, 1000); }, formatDate: (time) => { const date = time?new Date(time):new Date(); const hours = date.getHours().toString().padStart(2, "0"); const minutes = date.getMinutes().toString().padStart(2, "0"); return ${hours}:${minutes}; }, showKeyboard() { state.isKeyboardVisible = true; document.querySelector("input").focus(); }, hideKeyboard() { state.isKeyboardVisible = false; document.activeElement.focus(); }, // handleScroll() { // const chatList = this.$refs.chatList; // const scrollTop = chatList.scrollTop; // const scrollHeight = chatList.scrollHeight; // const clientHeight = chatList.clientHeight; // if (scrollTop === 0) { // // 聊天列表滚动到了顶部,需要加载更多聊天记录 // // TODO: 加载更多聊天记录 // // 滚动到之前的位置,避免列表跳动 // chatList.scrollTop = chatList.scrollHeight - scrollHeight; // } else if (scrollTop + clientHeight >= scrollHeight) { // // 聊天列表滚动到了底部,需要将列表自动上滑 // chatList.scrollTop = chatList.scrollHeight - clientHeight; // } // }, }); onMounted(() => { state.messages[0].msg.time = state.formatDate() }) </script>在该段代码中增加列表置底后上滑
可以在聊天列表上添加一个ref属性,用于获取聊天列表的dom节点,然后在sendMessage和replyMessage方法中,将聊天列表的scrollTop设置为聊天列表的scrollHeight,实现自动置底。在handleScroll方法中,可以判断scrollTop是否为0,如果为0则说明聊天列表已经滚动到了顶部,可以加载更多聊天记录;如果scrollTop加上clientHeight大于等于scrollHeight,则说明聊天列表已经滚动到了底部,需要将列表自动上滑。以下是修改后的代码:
```
<template>
<div class="chat-container">
<div class="chat-header">在线客服</div>
<div class="chat-list" ref="chatList">
<div class="chat-message" v-for="message in state.messages" :key="message.id">
<div class="chat-content" :class="{'user': message.isUser}">
<div class="chat-text">{{ message.content }}</div>
<div class="chat-time">{{ state.formatDate(message.time) }}</div>
</div>
</div>
</div>
<div class="chat-input">
<div class="chat-input-box" :class="{'show-keyboard': state.isKeyboardVisible}">
<input type="text" v-model="state.inputText" placeholder="请输入您的问题" />
<div class="chat-input-btn" @click="state.sendMessage">发送</div>
<div class="chat-input-keyboard" @click="state.showKeyboard">键盘</div>
<div class="chat-input-voice">语音</div>
</div>
<div class="chat-input-keyboard-box" v-show="state.isKeyboardVisible">
<div class="chat-input-keyboard-item" v-for="(item, index) in state.keyboard" :key="index" @click="state.inputText += item">{{ item }}</div>
<div class="chat-input-keyboard-item" @click="state.inputText = state.inputText.slice(0, -1)">删除</div>
<div class="chat-input-keyboard-item" @click="state.hideKeyboard">收起</div>
</div>
</div>
</div>
</template>
<script setup>
import { reactive, onMounted } from "vue";
import useGetUrl from "@/hooks/useGetUrl";
const state = reactive({
inputText: "",
isKeyboardVisible: false,
messages: [
{
content: "施主你好,很高兴为您服务!",
time: new Date(),
isUser: false,
},
],
keyboard: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
sendMessage: () => {
const content = state.inputText.trim();
if (content) {
const time = new Date();
const message = {
id: state.messages.length + 1,
content,
time,
isUser: true,
};
state.messages.push(message);
state.inputText = "";
state.replyMessage();
state.scrollToBottom();
}
},
replyMessage: () => {
const content = "尊敬的施主,您好。我们已收到您的咨询,请稍后。";
const time = new Date();
const message = {
id: state.messages.length + 1,
content,
time,
isUser: false,
};
setTimeout(() => {
state.messages.push(message);
state.scrollToBottom();
}, 1000);
},
formatDate: (time) => {
const date = time ? new Date(time) : new Date();
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
return `${hours}:${minutes}`;
},
showKeyboard() {
state.isKeyboardVisible = true;
document.querySelector("input").focus();
},
hideKeyboard() {
state.isKeyboardVisible = false;
document.activeElement.focus();
},
scrollToBottom() {
const chatList = this.$refs.chatList;
chatList.scrollTop = chatList.scrollHeight;
},
});
onMounted(() => {
state.messages[0].time = state.formatDate();
});
</script>
<style scoped>
.chat-container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.chat-header {
height: 40px;
line-height: 40px;
background-color: #f5f5f5;
text-align: center;
}
.chat-list {
flex: 1;
overflow-y: auto;
padding: 10px;
}
.chat-message {
margin-bottom: 10px;
}
.chat-content {
display: inline-block;
padding: 5px 10px;
border-radius: 10px;
max-width: 80%;
word-wrap: break-word;
}
.chat-content.user {
text-align: right;
background-color: #e6f7ff;
}
.chat-content.user .chat-text {
display: inline-block;
text-align: left;
}
.chat-content:not(.user) {
text-align: left;
background-color: #fff;
}
.chat-time {
font-size: 12px;
color: #999;
text-align: right;
margin-top: 5px;
}
.chat-input {
height: 50px;
border-top: 1px solid #ddd;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.chat-input-box {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
bottom: 0;
left: 0;
padding: 5px;
}
.chat-input-btn {
background-color: #409eff;
color: #fff;
padding: 5px 10px;
border-radius: 5px;
margin-left: 10px;
cursor: pointer;
}
.chat-input-keyboard {
font-size: 20px;
margin-left: 10px;
cursor: pointer;
}
.chat-input-voice {
font-size: 20px;
margin-left: 10px;
cursor: pointer;
}
.chat-input-keyboard-box {
width: 100%;
display: none;
position: absolute;
bottom: 50px;
left: 0;
background-color: #fff;
border-top: 1px solid #ddd;
}
.chat-input-keyboard-box.show {
display: block;
}
.chat-input-keyboard-item {
display: inline-block;
width: calc(100% / 3);
height: 40px;
line-height: 40px;
text-align: center;
cursor: pointer;
}
.chat-input-keyboard-item:nth-child(10) {
width: calc(100% / 3 * 2);
}
.show-keyboard .chat-input-keyboard-box {
display: block;
}
</style>
```