基于Vue3實(shí)現(xiàn)一個(gè)小相冊(cè)詳解
下面是是對(duì)Vue3操作的一個(gè)項(xiàng)目實(shí)戰(zhàn)
下面代碼是html的基本骨架(沒(méi)有任何的功能):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相冊(cè)</title>
<style>
.box{
margin-bottom: 20px;
padding: 0;
}
.img{
width: 480px;
height: 240px;
border: 1px bisque solid;
}
</style>
</head>
<body>
<div id="app">
<h2>基于Vue3實(shí)現(xiàn)的相冊(cè):展示第xx張相片</h2>
<img src = "./img_src/logo1.png" class="img" alt="圖片加載失敗">
<ul type="none" class="box"></ul>
<button>上一張</button> <button>下一張</button>
</div>
<script type="module">
import { createApp, ref } from './vue.esm-browser.js'
</script>
</body>
</html>運(yùn)行結(jié)果:

接下來(lái)我們將添加代碼使其變成一個(gè)小相冊(cè),運(yùn)行結(jié)果如下圖:




我們可以點(diǎn)擊上一張或下一張來(lái)實(shí)現(xiàn)圖片的跳轉(zhuǎn),也可以使用按鈕1234來(lái)實(shí)現(xiàn)你想跳轉(zhuǎn)的張數(shù)
【實(shí)現(xiàn)思路】
1. 利用v-on為切換相片的按鈕綁定上一個(gè)函數(shù),這個(gè)函數(shù)負(fù)責(zé)更改圖片路徑
2. 利用v-bind綁定圖片的路徑,使得圖片路徑可以自動(dòng)更新
以下是實(shí)現(xiàn)相冊(cè)的完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相冊(cè)</title>
<style>
.clear_ele::after{
content: ""; /* 這個(gè)偽元素的內(nèi)容屬性必須有 */
display: block;
clear: both;
}
.box{
margin-bottom: 20px;
padding: 0;
}
.button{
background-color: bisque;
width: 20px;
float: left;
text-align: center;
margin-right: 10px;
border-radius: 8px;
cursor: pointer;
}
.img{
width: 480px;
height: 240px;
border: 1px bisque solid;
}
</style>
</head>
<body>
<div id="app">
<h2>基于Vue3實(shí)現(xiàn)的相冊(cè):展示第{{ img.number }}張相片</h2>
<img v-bind:src="img.url" class="img">
<ul type="none" class="clear_ele box">
<li v-for="(val, idx) in 4" @click="jump(val)" class="button"> {{val}} </li>
</ul>
<button @click="prev">上一張</button> <button @click="next">下一張</button>
</div>
<script type="module">
import { createApp, ref, reactive } from './vue.esm-browser.js'
createApp({
setup() {
// 【定義數(shù)據(jù)】
const img = reactive(
{
number: 1,
url: "./img_src/logo1.png"
}
)
// 【定義函數(shù)】
//上一張
const prev = () => {
img.number--
if (img.number == 0) {
img.number = 4
}
img.url = `./img_src/logo${img.number}.png`
}
//下一張
const next = () => {
img.number++
if (img.number == 5) {
img.number = 1
}
img.url = `./img_src/logo${img.number}.png`
}
//跳轉(zhuǎn)
const jump = (val) => {
img.number = val
img.url = `./img_src/logo${img.number}.png`
}
return {img, prev,next,jump}
}
}).mount("#app")
</script>
</body>
</html>還有另一種方法也同樣可以實(shí)現(xiàn)相冊(cè)的效果,代碼如下:
下述代碼的弊端就是比較冗長(zhǎng),相對(duì)于上一種方法會(huì)消耗更長(zhǎng)時(shí)間,因?yàn)樗前衙恳粡堈掌氖褂媒Y(jié)果一一敲出來(lái)的,可與上面代碼比對(duì)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相冊(cè)</title>
<style>
.clear_ele::after{
content: ""; /* 這個(gè)偽元素的內(nèi)容屬性必須有 */
display: block;
clear: both; /* 忽略前面盒子浮動(dòng)帶來(lái)的影響,解決父盒高度塌陷問(wèn)題 */
}
.button{
background-color: bisque;
width: 20px;
float: left;
text-align: center;
margin-right: 10px;
border-radius: 8px;
cursor: pointer;
}
.img{
width: 480px;
height: 240px;
border: 1px bisque solid;
}
.box{
margin-bottom: 20px;
padding: 0;
}
</style>
</head>
<body>
<div id="app">
<h2>基于Vue3實(shí)現(xiàn)的相冊(cè):展示第{{img.index}}張相片</h2>
<img v-bind:src= "img.url" class="img" alt="圖片加載失敗">
<ul type="none" class="box clear_ele">
<li class="button" v-on:click = "show_1_img">1</li>
<li class="button" v-on:click = "show_2_img">2</li>
<li class="button" v-on:click = "show_3_img">3</li>
<li class="button" v-on:click = "show_4_img">4</li>
</ul>
<button v-on:click = "pre">上一張</button>
<button v-on:click = "next">下一張</button>
</div>
<script type="module">
import { createApp, reactive } from './vue.esm-browser.js'
createApp({
setup() {
const img = reactive(
{
index: 1,
url: "./img_src/logo1.png", //圖片路徑
}
)
const show_1_img = () => {
img.index = 1
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊第${img.index}張按鈕,顯示第${img.index}張照片`);
}
const show_2_img = () => {
img.index = 2
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊第${img.index}張按鈕,顯示第${img.index}張照片`);
}
const show_3_img = () => {
img.index = 3
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊第${img.index}張按鈕,顯示第${img.index}張照片`);
}
const show_4_img = () => {
img.index = 4
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊第${img.index}張按鈕,顯示第${img.index}張照片`);
}
const pre = () => {
img.index = img.index - 1
if(img.index < 1 ){
img.index = 4
}
// 把圖片路徑存儲(chǔ)在響應(yīng)式數(shù)據(jù)里,當(dāng)這個(gè)響應(yīng)式數(shù)據(jù)改變時(shí),html的圖片路徑就會(huì)自動(dòng)改變
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊了上一張按鈕,顯示第${img.index}張照片`);
}
const next = () => {
img.index = img.index + 1
if(img.index > 4 ){ // 圖片展示完了,回到第一張
img.index = 1
}
// 把圖片路徑存儲(chǔ)在響應(yīng)式數(shù)據(jù)里,當(dāng)這個(gè)響應(yīng)式數(shù)據(jù)改變時(shí),html的圖片路徑就會(huì)自動(dòng)改變
img.url = `./img_src/logo${img.index}.png`
console.log(`用戶點(diǎn)擊了下一張按鈕,顯示第${img.index}張照片`);
}
return { img,
show_1_img,
show_2_img,
show_3_img,
show_4_img,
pre, next } //把數(shù)據(jù)(屬性), 函數(shù)(方法)暴露出來(lái),供HTML模板調(diào)用
}
}).mount("#app")
</script>
</body>
</html>到此這篇關(guān)于基于Vue3實(shí)現(xiàn)一個(gè)小相冊(cè)詳解的文章就介紹到這了,更多相關(guān)Vue3相冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析
這篇文章主要介紹了基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Element?Plus?去掉表格外邊框的實(shí)現(xiàn)代碼
使用el-table組件拖拽時(shí),?想使用自定義樣式進(jìn)行拖拽,?想去掉外邊框,?并在表頭加入豎杠樣式,本文給大家介紹Element?Plus?去掉表格外邊框的實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2025-04-04
Vue全局注冊(cè)中的kebab-case和PascalCase用法
這篇文章主要介紹了Vue全局注冊(cè)中的kebab-case和PascalCase用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
Vue.js@2.6.10更新內(nèi)置錯(cuò)誤處機(jī)制Fundebug同步支持相應(yīng)錯(cuò)誤監(jiān)控
這篇文章主要介紹了Vue.js@2.6.10更新內(nèi)置錯(cuò)誤處機(jī)制,F(xiàn)undebug同步支持相應(yīng)錯(cuò)誤監(jiān)控 ,需要的朋友可以參考下2019-05-05
vue數(shù)據(jù)更新了但在頁(yè)面上沒(méi)有顯示出來(lái)的解決方法
有時(shí)候 vue 無(wú)法監(jiān)聽(tīng)到數(shù)據(jù)的變化,導(dǎo)致數(shù)據(jù)變化但是視圖沒(méi)有變化,也就是數(shù)據(jù)更新了,但在頁(yè)面上沒(méi)有顯示出來(lái),所以本文給出了三種解決方法,通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Vue+ElementUI創(chuàng)建一個(gè)帶有進(jìn)度顯示的文件下載和打包組件功能
如何使用 Vue 創(chuàng)建一個(gè)帶有進(jìn)度顯示和打包功能的文件下載組件,我們探討了如何導(dǎo)入必要的包,構(gòu)建組件的基礎(chǔ)結(jié)構(gòu),實(shí)現(xiàn)文件下載與進(jìn)度顯示,以及如何將文件打包為 ZIP 格式供用戶下載2024-08-08
Vue data的數(shù)據(jù)響應(yīng)式到底是如何實(shí)現(xiàn)的
這篇文章主要介紹了Vue data的數(shù)據(jù)響應(yīng)式到底是如何實(shí)現(xiàn)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

