メディアコンポーネント
メディアコンポーネントは、動画の再生と画像のプレビュー機能を提供します。
動画
基本的な使い方
html
<video
id="myVideo"
src="https://example.com/video.mp4"
poster="https://example.com/poster.jpg"
controls
style="width: 100%;"
/>プログラムからの制御
createVideoContext で動画のコンテキストを取得します。
ts
const videoCtx = woo.createVideoContext('myVideo')
// 播放
videoCtx.play()
// 暂停
videoCtx.pause()
// 跳转到指定时间(秒)
videoCtx.seek(30)
// 全屏
videoCtx.requestFullScreen()
videoCtx.exitFullScreen()完全な例
vue
<template>
<div class="page">
<video
id="courseVideo"
:src="videoUrl"
:poster="posterUrl"
style="width: 100%; border-radius: 8px;"
/>
<div class="controls">
<button @click="togglePlay">
{{ isPlaying ? '暂停' : '播放' }}
</button>
<button @click="jumpForward">快进 10s</button>
<button @click="fullscreen">全屏</button>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import woo from 'mini-sdk'
const videoUrl = ref('https://example.com/course.mp4')
const posterUrl = ref('https://example.com/poster.jpg')
const isPlaying = ref(false)
let videoCtx = null
let currentTime = 0
onMounted(() => {
videoCtx = woo.createVideoContext('courseVideo')
})
function togglePlay() {
if (isPlaying.value) {
videoCtx.pause()
} else {
videoCtx.play()
}
isPlaying.value = !isPlaying.value
}
function jumpForward() {
currentTime += 10
videoCtx.seek(currentTime)
}
function fullscreen() {
videoCtx.requestFullScreen()
}
</script>画像プレビュー
woo.previewImage() で画像一覧を全画面表示します。
ts
await woo.previewImage({
urls: [
'https://example.com/photo1.jpg',
'https://example.com/photo2.jpg',
'https://example.com/photo3.jpg',
],
current: 'https://example.com/photo2.jpg', // 初始显示第 2 张
})完全な例:3×3 のグリッド
vue
<template>
<div class="image-grid">
<img
v-for="(url, index) in images"
:key="index"
:src="url"
@click="preview(index)"
class="grid-image"
/>
</div>
</template>
<script setup>
import woo from 'mini-sdk'
const images = [
'https://example.com/1.jpg',
'https://example.com/2.jpg',
'https://example.com/3.jpg',
'https://example.com/4.jpg',
'https://example.com/5.jpg',
'https://example.com/6.jpg',
]
function preview(index) {
woo.previewImage({
urls: images,
current: images[index],
})
}
</script>
<style scoped>
.image-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
padding: 16px;
}
.grid-image {
width: 100%;
aspect-ratio: 1;
object-fit: cover;
border-radius: 8px;
}
</style>入力フォーカス
createInputContext で入力欄のフォーカスを制御します。
ts
const inputCtx = woo.createInputContext('searchInput')
// 自动聚焦
inputCtx.focus()
// 失焦
inputCtx.blur()ノードの取得
createSelectorQuery でノード情報を取得します。
ts
const query = woo.createSelectorQuery()
// 查询单个节点
const rect = await query.select('#header').boundingClientRect()
console.log('Header 高度:', rect.height)
// 查询滚动位置
const scroll = await query.select('#scrollView').scrollOffset()
console.log('滚动距离:', scroll.scrollTop)