<template>
<div class="component-card-demo-wrap">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '16px', y: '16px' }">
<div class="demo-box">可拖动的盒子</div>
</TsFloatBox>
</div>
</template>
<script setup lang="ts">
import { TsFloatBox } from 'tui';
</script>
<style scoped>
.component-card-demo-wrap {
height: 220px;
position: relative;
border: 1px dashed #e5e7eb;
border-radius: 8px;
}
.demo-box {
width: 100px;
height: 100px;
background: #f0f9eb;
display: flex;
align-items: center;
justify-content: center;
border-radius: 6px;
color: #2f855a;
}
</style>
<template>
<div class="component-card-demo-wrap">
<div class="demo-container">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '20px', y: '20px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box">边界限制</div>
</TsFloatBox>
</div>
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
.component-card-demo-wrap {
height: 300px;
}
.demo-container {
height: 100%;
border: 1px solid #eee;
border-radius: 4px;
position: relative;
}
.demo-box {
width: 100px;
height: 100px;
background: #f0f9eb;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
color: #67c23a;
font-weight: 500;
}
</style>
<template>
<div class="component-card-demo-wrap">
<div class="demo-container">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '50%', y: '50%' }">
<div class="demo-box custom-box">居中显示</div>
</TsFloatBox>
</div>
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
.component-card-demo-wrap {
height: 200px;
}
.demo-container {
height: 100%;
border: 1px solid #eee;
border-radius: 4px;
position: relative;
}
.demo-box {
width: 100px;
height: 100px;
background: #f0f9eb;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
color: #67c23a;
font-weight: 500;
}
.custom-box {
width: 120px;
height: 120px;
border-radius: 50%;
color: #67c23a;
font-weight: bold;
}
</style>
<template>
<div class="component-card-demo-wrap">
<div class="demo-container">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '20px', y: '20px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box box-1">浮动框 1</div>
</TsFloatBox>
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '150px', y: '80px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box box-2">浮动框 2</div>
</TsFloatBox>
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '80px', y: '140px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box box-3">浮动框 3</div>
</TsFloatBox>
</div>
</div>
</template>
<script setup lang="ts">
import { TsFloatBox } from 'tui';
</script>
<style scoped>
.component-card-demo-wrap {
height: 300px;
}
.demo-container {
height: 100%;
border: 1px solid #eee;
border-radius: 4px;
position: relative;
}
.demo-box {
width: 80px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
font-weight: 500;
font-size: 12px;
color: white;
}
.box-1 {
background: #409eff;
}
.box-2 {
background: #67c23a;
}
.box-3 {
background: #e6a23c;
}
</style>
<template>
<div class="component-card-demo-wrap">
<div class="demo-container">
<TsFloatBox
position-mode="absolute"
:initialPosition="{ x: '20px', y: '20px' }"
:boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }"
@drag-start="handleDragStart"
@drag-move="handleDragMove"
@drag-end="handleDragEnd"
>
<div class="demo-box">事件监听</div>
</TsFloatBox>
</div>
<div class="event-log">
<h4>拖动事件</h4>
<div class="log-item" v-for="(log, index) in eventLogs" :key="index">
<span class="log-time">{{ log.time }}</span>
<span class="log-event">{{ log.event }}</span>
<span class="log-position">{{ log.position }}</span>
</div>
</div>
<div class="demo-actions">
<TsButton @click="clearLogs" size="small">清空日志</TsButton>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { TsButton, TsFloatBox } from 'tui';
const eventLogs = ref<Array<{time: string, event: string, position: string}>>([]);
const handleDragStart = (event: any) => {
addLog('开始拖动', `(${event.x}, ${event.y})`);
};
const handleDragMove = (event: any) => {
if (eventLogs.value.length === 0 || eventLogs.value[0].event !== '拖动中') {
addLog('拖动中', `(${event.x}, ${event.y})`);
}
};
const handleDragEnd = (event: any) => {
addLog('拖动结束', `(${event.x}, ${event.y})`);
};
const addLog = (event: string, position: string) => {
const now = new Date();
const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
eventLogs.value.unshift({
time,
event,
position
});
// 只保留最近10条记录
if (eventLogs.value.length > 10) {
eventLogs.value = eventLogs.value.slice(0, 10);
}
};
const clearLogs = () => {
eventLogs.value = [];
};
</script>
<style scoped>
.component-card-demo-wrap {
height: 300px;
}
.demo-container {
height: 200px;
border: 1px solid #eee;
border-radius: 4px;
position: relative;
margin-bottom: 16px;
}
.demo-box {
width: 100px;
height: 100px;
background: #f0f9eb;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
color: #67c23a;
font-weight: 500;
}
.event-log {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 6px;
padding: 16px;
margin-bottom: 16px;
max-height: 200px;
overflow-y: auto;
}
.event-log h4 {
margin: 0 0 12px 0;
font-size: 16px;
color: #333;
}
.log-item {
display: flex;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #e8e8e8;
font-size: 13px;
}
.log-item:last-child {
border-bottom: none;
}
.log-time {
color: #999;
font-family: monospace;
margin-right: 12px;
min-width: 60px;
}
.log-event {
color: #1890ff;
font-weight: 500;
margin-right: 12px;
min-width: 80px;
}
.log-position {
color: #52c41a;
font-family: monospace;
}
.demo-actions {
margin-top: 16px;
}
</style>
不同形状和样式
实际应用场景
<template>
<div class="component-card-demo-wrap">
<div class="demo-section">
<p class="demo-title">不同形状和样式</p>
<div class="demo-container">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '20px', y: '20px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box square">方形</div>
</TsFloatBox>
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '120px', y: '60px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box circle">圆形</div>
</TsFloatBox>
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '220px', y: '100px' }" :boundaries="{ minX: '20', minY: '20', maxX: '280', maxY: '180' }">
<div class="demo-box rounded">圆角</div>
</TsFloatBox>
</div>
</div>
<div class="demo-section">
<p class="demo-title">实际应用场景</p>
<div class="demo-container">
<TsFloatBox position-mode="absolute" :initialPosition="{ x: '10px', y: '10px' }" :boundaries="{ minX: '10', minY: '10', maxX: '290', maxY: '190' }">
<div class="widget-box">
<div class="widget-header">
<span class="widget-title">悬浮工具</span>
<button class="widget-close" @click="toggleWidget">×</button>
</div>
<div class="widget-content">
<button class="widget-btn" @click="showMessage('刷新')">🔄</button>
<button class="widget-btn" @click="showMessage('设置')">⚙️</button>
<button class="widget-btn" @click="showMessage('帮助')">❓</button>
</div>
</div>
</TsFloatBox>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const widgetVisible = ref(true);
const toggleWidget = () => {
widgetVisible.value = !widgetVisible.value;
};
const showMessage = (action: string) => {
console.log(`执行操作: ${action}`);
};
</script>
<style scoped>
.component-card-demo-wrap {
height: 400px;
}
.demo-section {
margin-bottom: 32px;
}
.demo-title {
font-weight: bold;
margin-bottom: 12px;
color: var(--ts-color-text);
}
.demo-container {
height: 150px;
border: 1px solid #eee;
border-radius: 4px;
position: relative;
margin-bottom: 16px;
}
.demo-box {
width: 60px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: 500;
color: white;
cursor: move;
user-select: none;
}
.square {
background: #409eff;
border-radius: 4px;
}
.circle {
background: #67c23a;
border-radius: 50%;
}
.rounded {
background: #e6a23c;
border-radius: 12px;
}
.widget-box {
width: 180px;
background: white;
border: 1px solid #dcdfe6;
border-radius: 6px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.widget-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 12px;
background: #f5f7fa;
border-bottom: 1px solid #ebeef5;
}
.widget-title {
font-size: 14px;
font-weight: 500;
color: #303133;
}
.widget-close {
background: none;
border: none;
font-size: 16px;
color: #909399;
cursor: pointer;
padding: 0;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 2px;
}
.widget-close:hover {
background: #f56c6c;
color: white;
}
.widget-content {
padding: 12px;
display: flex;
gap: 8px;
}
.widget-btn {
width: 40px;
height: 40px;
border: 1px solid #dcdfe6;
border-radius: 4px;
background: white;
cursor: pointer;
font-size: 16px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s;
}
.widget-btn:hover {
border-color: #409eff;
color: #409eff;
transform: scale(1.05);
}
</style>