给我一个.vue顶栏可以添加表情,插入图片可以修改字体的日记代码
时间: 2023-06-27 14:03:11 浏览: 102
以下是一个简单的 Vue 顶栏组件,可以添加表情和插入图片,并且可以修改字体:
```html
<template>
<div class="topbar">
<div class="emoji-btn" @click="toggleEmojiPicker">
<i class="far fa-smile"></i> 表情
</div>
<div class="img-btn" @click="openImagePicker">
<i class="far fa-image"></i> 插入图片
</div>
<div class="font-dropdown">
<div class="font-btn" @click="toggleFontDropdown">
<i class="fas fa-font"></i> 字体
</div>
<ul v-show="showFontDropdown">
<li><a href="#" @click.prevent="setFont('Arial')">Arial</a></li>
<li><a href="#" @click.prevent="setFont('Times New Roman')">Times New Roman</a></li>
<li><a href="#" @click.prevent="setFont('Verdana')">Verdana</a></li>
</ul>
</div>
<div class="emoji-picker" v-show="showEmojiPicker">
<ul>
<li @click="insertEmoji('😀')"><span>😀</span></li>
<li @click="insertEmoji('😂')"><span>😂</span></li>
<li @click="insertEmoji('😍')"><span>😍</span></li>
<li @click="insertEmoji('👍')"><span>👍</span></li>
<li @click="insertEmoji('👎')"><span>👎</span></li>
<li @click="insertEmoji('🤔')"><span>🤔</span></li>
</ul>
</div>
<input type="file" ref="imageInput" style="display: none" @change="insertImage">
</div>
</template>
<script>
export default {
data() {
return {
showEmojiPicker: false,
showFontDropdown: false,
currentFont: 'Arial'
}
},
methods: {
toggleEmojiPicker() {
this.showEmojiPicker = !this.showEmojiPicker;
},
toggleFontDropdown() {
this.showFontDropdown = !this.showFontDropdown;
},
setFont(font) {
this.currentFont = font;
this.showFontDropdown = false;
},
insertEmoji(emoji) {
document.execCommand('insertText', false, emoji);
},
openImagePicker() {
this.$refs.imageInput.click();
},
insertImage() {
const file = this.$refs.imageInput.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imgData = canvas.toDataURL(file.type);
document.execCommand('insertHTML', false, `<img src="${imgData}" style="max-width: 100%;"/>`);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
}
}
</script>
<style>
.topbar {
display: flex;
align-items: center;
height: 50px;
background-color: #eee;
padding: 0 20px;
}
.emoji-btn, .img-btn, .font-btn {
margin-right: 10px;
cursor: pointer;
}
.emoji-picker {
position: absolute;
top: 50px;
left: 0;
width: 100%;
background-color: #fff;
border: 1px solid #ccc;
}
.emoji-picker ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.emoji-picker li {
display: flex;
justify-content: center;
align-items: center;
width: 30px;
height: 30px;
margin: 5px;
border-radius: 50%;
background-color: #f0f0f0;
cursor: pointer;
}
.emoji-picker li:hover {
background-color: #e0e0e0;
}
.emoji-picker li span {
font-size: 20px;
}
.font-dropdown {
position: relative;
}
.font-dropdown ul {
list-style: none;
margin: 0;
padding: 0;
position: absolute;
top: 100%;
left: 0;
background-color: #fff;
border: 1px solid #ccc;
z-index: 1;
display: none;
}
.font-dropdown ul li {
padding: 5px;
}
.font-dropdown ul li a {
color: #333;
text-decoration: none;
}
.font-dropdown ul li:hover {
background-color: #f0f0f0;
}
.font-dropdown ul li:hover a {
color: #000;
}
.font-dropdown ul:after {
content: "";
display: block;
position: absolute;
top: -10px;
left: 50%;
transform: translateX(-50%);
border: 10px solid transparent;
border-bottom-color: #ccc;
}
.font-dropdown ul:before {
content: "";
display: block;
position: absolute;
top: -11px;
left: 50%;
transform: translateX(-50%);
border: 10px solid transparent;
border-bottom-color: #fff;
}
.font-btn i {
margin-right: 5px;
}
input[type="file"] {
display: none;
}
</style>
```
你可以将此组件添加到你的 Vue 应用中,然后在需要使用顶栏的地方引入使用即可。
阅读全文