扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章给大家介绍怎么在Vue中实现一个展开折叠效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联主营太和网站建设的网络公司,主营网站建设方案,App定制开发,太和h5微信小程序搭建,太和网站营销推广欢迎太和等地区企业咨询
1.html代码
js文本段落展开和收拢效果 这一段文字是可以折叠展开的,点击下面的“更多”就可以展开,点击下面的“折叠”就可以折叠
2.js代码,最关键的
$(function() { var slideHeight = 45; // px 定义折叠的最小高度 var defHeight = $('#wrap').height(); if(defHeight >= slideHeight) { $('#wrap').css('height', slideHeight + 'px'); $('#read-more').append('更多'); $('#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的方式实现上述效果,同样可以在Vue实现,下面是解决办法:
1、创建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组件中引入
欢迎大家品尝Pizza!
这里有你非常喜欢的Pizza!
关于怎么在Vue中实现一个展开折叠效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流