Vue 實(shí)現(xiàn)展開(kāi)折疊效果的示例代碼
本文介紹了Vue 實(shí)現(xiàn)展開(kāi)折疊效果的示例代碼,分享給大家,具體如下:
效果如見(jiàn):

1.html代碼
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js文本段落展開(kāi)和收攏效果</title>
<script type="text/javascript" src="http://www.dbjr.com.cn/ajaxjs/jquery-1.6.2.min.js"></script>
</head>
<body>
<div id="container">
<div id="wrap">
<div>
<h1>這一段文字是可以折疊展開(kāi)的,點(diǎn)擊下面的“更多”就可以展開(kāi),點(diǎn)擊下面的“折疊”就可以折疊</h1>
</div>
</div>
<div id="read-more"></div>
</div>
</body>
</html>
2.js代碼,最關(guān)鍵的
$(function() {
var slideHeight = 45; // px 定義折疊的最小高度
var defHeight = $('#wrap').height();
if(defHeight >= slideHeight) {
$('#wrap').css('height', slideHeight + 'px');
$('#read-more').append('<a href="#" rel="external nofollow" >更多</a>');
$('#read-more a').click(function() {
var curHeight = $('#wrap').height();
if(curHeight == slideHeight) {
$('#wrap').animate({
height: defHeight
}, "normal");
$('#read-more a').html('折疊');
} else {
$('#wrap').animate({
height: slideHeight
}, "normal");
$('#read-more a').html('更多');
}
return false;
});
}
});
3.css代碼
#container {
margin: 0 auto;
width: 600px;
border: 1px solid #3bb2d0;
}
#wrap {
position: relative;
padding: 10px;
overflow: hidden;
}
#read-more {
padding: 5px;
background: #fff;
color: #333;
}
#read-more a {
padding-right: 22px;
no-repeat 100% 50%;
font-weight: bold;
text-decoration: none;
}
#read-more a: hover {
color: #000;
}
除了使用jQuery的方式實(shí)現(xiàn)上述效果,同樣可以在Vue實(shí)現(xiàn),下面是解決辦法:
1、創(chuàng)建collapse.js文件
const elTransition =
"0.3s height ease-in-out, 0.3s padding-top ease-in-out, 0.3s padding-bottom ease-in-out";
const Transition = {
"before-enter"(el) {
el.style.transition = elTransition;
if (!el.dataset) el.dataset = {};
el.dataset.oldPaddingTop = el.style.paddingTop;
el.dataset.oldPaddingBottom = el.style.paddingBottom;
el.style.height = 0;
el.style.paddingTop = 0;
el.style.paddingBottom = 0;
},
enter(el) {
el.dataset.oldOverflow = el.style.overflow;
if (el.scrollHeight !== 0) {
el.style.height = el.scrollHeight + "px";
el.style.paddingTop = el.dataset.oldPaddingTop;
el.style.paddingBottom = el.dataset.oldPaddingBottom;
} else {
el.style.height = "";
el.style.paddingTop = el.dataset.oldPaddingTop;
el.style.paddingBottom = el.dataset.oldPaddingBottom;
}
el.style.overflow = "hidden";
},
"after-enter"(el) {
el.style.transition = "";
el.style.height = "";
el.style.overflow = el.dataset.oldOverflow;
},
"before-leave"(el) {
if (!el.dataset) el.dataset = {};
el.dataset.oldPaddingTop = el.style.paddingTop;
el.dataset.oldPaddingBottom = el.style.paddingBottom;
el.dataset.oldOverflow = el.style.overflow;
el.style.height = el.scrollHeight + "px";
el.style.overflow = "hidden";
},
leave(el) {
if (el.scrollHeight !== 0) {
el.style.transition = elTransition;
el.style.height = 0;
el.style.paddingTop = 0;
el.style.paddingBottom = 0;
}
},
"after-leave"(el) {
el.style.transition = "";
el.style.height = "";
el.style.overflow = el.dataset.oldOverflow;
el.style.paddingTop = el.dataset.oldPaddingTop;
el.style.paddingBottom = el.dataset.oldPaddingBottom;
}
};
export default {
name: "collapseTransition",
functional: true,
render(h, { children }) {
const data = {
on: Transition
};
return h("transition", data, children);
}
};
2、在.vue組件中引入
<template>
<div class="container">
<button @click="isActive = !isActive">展開(kāi)/折疊</button>
<collapse>
<div class="container" v-show="isActive">
<h2>歡迎大家品嘗Pizza!</h2>
<h5>這里有你非常喜歡的Pizza!</h5>
</div>
</collapse>
</div>
</template>
<script>
import collapse from "../assets/js/collapse.js";
export default {
data() {
return {
isActive: false
};
},
components: {
collapse
}
};
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue使用Element折疊面板Collapse如何設(shè)置默認(rèn)全部展開(kāi)
- vue+antd實(shí)現(xiàn)折疊與展開(kāi)組件
- Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開(kāi)功能示例
- vue同個(gè)按鈕控制展開(kāi)和折疊同個(gè)事件操作
- vue-列表下詳情的展開(kāi)與折疊案例
- vue多級(jí)復(fù)雜列表展開(kāi)/折疊及全選/分組全選實(shí)現(xiàn)
- vuejs實(shí)現(xiàn)折疊面板展開(kāi)收縮動(dòng)畫(huà)效果
- vue:左右過(guò)渡展開(kāi)折疊的組件
相關(guān)文章
vue 折疊展示多行文本組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue 折疊展示多行文本組件,自動(dòng)根據(jù)傳入的expand判斷是否需要折疊,非常完美,文章通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
在Vue項(xiàng)目中使用d3.js的實(shí)例代碼
這篇文章主要介紹了在Vue項(xiàng)目中使用d3.js的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值價(jià)值,需要的朋友可以參考下2018-05-05
如何監(jiān)聽(tīng)Vue項(xiàng)目報(bào)錯(cuò)的4種方式?
本文主要介紹了如何監(jiān)聽(tīng)Vue項(xiàng)目報(bào)錯(cuò)的4種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
關(guān)于vue中watch檢測(cè)到不到對(duì)象屬性的變化的解決方法
本篇文章主要介紹了關(guān)于vue中watch檢測(cè)到不到對(duì)象屬性的變化的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue setInterval 定時(shí)器失效的解決方式
這篇文章主要介紹了vue setInterval 定時(shí)器失效的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

