1.是否在微信环境内打开链接
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
console.log('在微信浏览器内');
} else {
console.log('不在微信浏览器内');
return
}
2.# 小程序隐藏API - onAppRoute(eventListener)
在小程序切换页面或打开页面时会触发onAppRoute 事件,小程序框架通过wx.onAppRoute 可以注册页面切换时的处理程序,一般开发放在app.js的onLunch生命周期中全局注册一次即可,可用于监听页面切换。
onLaunch() {
wx.onAppRoute((route) => {
console.log(route);
});
}
通过对查看route,个人总结如下
数据实例
webviewId: 140,path: "pages/index/index"
query:t
openType:
"navigateBack
page:.
selectCommunity.
openType:"navigateBack"
page: (window:.
path:"pages/index/index'
query:
renderer:"webview"
singlePageData: {windowId: 140,pageid: 140,isNewEngineHomePage: true)
webviewId: 140
ts : 1691459595303
3. 复制功能
const textToCopy = this.userName;
const textArea = document.createElement('textarea');
textArea.value = textToCopy;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Failed to copy text:', err);
} finally {
document.body.removeChild(textArea);
}
3.拖拽事件
关于拖拽 drag & drop
拖放(Drag 和 drop)是 HTML5 标准的组成部分。
**拖拽对象
**
dataTransfer对象,只能在拖放事件的事件处理程序中访问。重要属性:
- effectAllowed ( none | copy | copyLink | copyMove | link、linkMove | move | all | uninitialized ):设置或返回被拖动元素允许发生的拖动行为。
- dropEffect( none | copy | link | move ):设置或返回拖放目标上允许发生的拖放行为。如果此设置的拖放行为不在effectAllowed属性设置的多种拖放行为之内,拖放操作将会失败。
- dataTransfer.getData(format):获取DataTransfer对象中设置format格式的数据。其中format代表数据格式,data代表数据。
拖拽属性
draggable 属性规定元素是否可拖动。
拖拽事件
- ondragstart:在拖动开始时执行,返回被拖动元素。
- ondragover:返回在何处放置被拖动的数据
- 默认地,无法将数据/元素放置到其他元素中。如果需要设置允许放置,我们必须阻止对元素的默认处理方式
- ondragenter:在被拖动的元素进入到放置目标时执行
- ondragleave:在被拖动的元素离开放置目标时执行
- ondragend && ondrop:皆指鼠标松开被拖动对象的事件,但是返回的分别为被拖动对象和被拖动元素悬挂的那个元素
源码:
<template>
<div class="transition-container">
<div class="item" v-for="(item, index) in items" :key="index"
draggable="true"
@dragstart="handleDragStart($event, item)"
@dragover.prevent="handleDragOver($event, item)"
@dragenter="handleDragEnter($event, item)"
@dragend="handleDragEnd($event, item)"
@click="chooseNav(item)"
>
<p class="trans-btn">
<span v-if="item.problemId">
<b class="id">
{{item.problemId}}
</b>
{{item.key}}
</span>
<span v-else>
{{item.key}}
</span>
<span>
<i-button v-if="btn" size="small" type="error" style="margin-right: 10px;" @click="deleteItem(item, index)">删除</i-button>
</span>
</p>
</div>
</div>
</template>
<script>
import './index.less';
export default {
name: 'transition',
props: {
dataSource: Array,
btn: Boolean,
},
data() {
return {
items: [],
dragging: null,
};
},
watch: {
dataSource(val) {
this.items = val;
},
dragging(val) {
if (this.dataSource.includes(val)) {
this.dragging = val;
} else {
this.dragging = null;
}
},
},
methods: {
handleDragStart(e, item) {
this.dragging = item;
},
handleDragEnd() {
this.dragging = null;
this.$emit('hasChanged', this.items);
},
// 首先把div变成可以放置的元素,即重写dragenter/dragover
handleDragOver(e) {
e.dataTransfer.dropEffect = 'move';// e.dataTransfer.dropEffect="move";//在dragenter中针对放置目标来设置!
},
handleDragEnter(e, item) {
if (this.dragging) {
e.dataTransfer.effectAllowed = 'move';// 为需要移动的元素设置dragstart事件
if (item === this.dragging) {
return;
}
const newItems = [...this.items];
console.log(newItems);
const src = newItems.indexOf(this.dragging);
const dst = newItems.indexOf(item);
newItems.splice(dst, 0, ...newItems.splice(src, 1));
this.items = newItems;
}
},
chooseNav(val) {
this.$emit('selectItem', val);
},
deleteItem(item, index) {
this.$emit('deleteItem', item, index);
},
editor(item, index) {
this.$emit('editorItem', item, index);
},
},
};
</script>
基本功能就完成啦
实战一下多选框单选框,数据替换就行
onDragStart(event, qIndex, oIndex) {
// 将拖动数据存储在事件中
event.dataTransfer.setData('qIndex', qIndex);
event.dataTransfer.setData('oIndex', oIndex);
},
onDragOver(event) {
event.preventDefault();
},
onDrop(event, qIndex, oIndex) {
event.preventDefault();
const originQIndex = event.dataTransfer.getData('qIndex');
const originOIndex = event.dataTransfer.getData('oIndex');
// 如果原始问题和当前问题不相同,才进行位置交换
if (originQIndex !== qIndex) {
// 将选项从原始问题中移除
const movedOption = this.questions[originQIndex].options.splice(originOIndex, 1)[0];
// 将选项插入到当前问题的指定位置
this.questions[qIndex].options.splice(oIndex, 0, movedOption);
}
},
<el-radio-group v-model="question.answer" @change="handleRadioChange(question, qqq)">
<div v-for="(option, optionIndex) in questions[qqq].options" :key="optionIndex"
class="option-container" draggable @dragstart="onDragStart($event, qqq, optionIndex)"
@dragover="onDragOver" @drop="onDrop($event, qqq, optionIndex)">
<!-- 添加一个拖动按钮,可以使用图标或其他元素来表示 -->
<el-radio :label="option.value">
<template v-if="option.editable">
<span class="label"
@click.prevent="editOption(qqq, question, optionIndex, question.options)">
{{ option.label }}
</span>
</template>
<template v-if="activeINdex == qqq && !option.editable">
<el-input v-model="questions[qqq].options[optionIndex].label" size="mini"
@blur="saveOption(qqq, optionIndex, $event)">
</el-input>
</template>
<span v-if="activeINdex == qqq && !option.editable" class="delete-icon"
@click="deleteradio(qqq, optionIndex)"><i class="el-icon-delete"
style="font-size: 16px;"></i></span>
</el-radio>
</div>
</el-radio-group>