html页面如何在图片上添加文字内容

html页面如何在图片上添加文字内容

添加一个DIV,采用绝对定位,图片所属DIV为基准

<div style="position:relativewidth:100pxheight:100px">

   <img src="图片地址" alt="" />

      <div style="position:absolutewidth:100pxheight:100pxz-indent:2left:0top:0">

       文字内容

      </div>

</div>

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 可以通过监听页面的 `mouseup` 事件实现在页面上选中文字添加批注的功能。具体实现步骤如下: 1. 监听 `mouseup` 事件,获取选中的文字。 ```html <template> <div ref="content" @mouseup="handleMouseUp"> {{ content }} </div> </template> ``` ```javascript export default { data() { return { content: '这是一段文字,可以被选中' } }, methods: { handleMouseUp() { const selection = window.getSelection() const selectedText = selection.toString().trim() if (selectedText) { // 选中了文字 } } } } ``` 2. 在选中的文字添加批注。 ```html <template> <div ref="content" @mouseup="handleMouseUp"> {{ content }} <div v-if="showComment" class="comment-box"> <textarea v-model="comment"></textarea> <button @click="saveComment">保存</button> </div> </div> </template> ``` ```javascript export default { data() { return { content: '这是一段文字,可以被选中', showComment: false, comment: '', selectedRange: null } }, methods: { handleMouseUp() { const selection = window.getSelection() const selectedText = selection.toString().trim() if (selectedText) { const range = selection.getRangeAt(0) this.selectedRange = range const rect = range.getBoundingClientRect() const top = rect.top + window.pageYOffset const left = rect.left + window.pageXOffset this.showComment = true this.$nextTick(() => { const commentBox = this.$el.querySelector('.comment-box') commentBox.style.top = `${top}px` commentBox.style.left = `${left}px` }) } else { this.showComment = false } }, saveComment() { const selection = window.getSelection() const range = this.selectedRange const comment = this.comment // 在选中的文字添加批注 const commentNode = document.createElement('span') commentNode.className = 'comment' commentNode.innerText = comment range.surroundContents(commentNode) this.showComment = false } } } ``` 以上是一个简单的实现,可以根据需求进行扩展和优化。需要注意的是,这种实现方式只能在页面内保存批注,刷新页面后批注会丢失。如果需要将批注保存到后台,需要通过后台接口进行处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值