vue展开收起组件
<template>
<div class="dynamic-wrap">
<div class="overlay" v-if="show_done" @click="show_done=false">
</div>
<div class="dynamic-top">
<div class="left">
<van-image @click="$router.push('/profile?id='+item.user_id)" class="avatar" fit="cover" lazy-load round :src="item.avatar" />
<div class="dynamic-by">
<div class="username" @click="$router.push('/profile?id='+item.user_id)">
{{item.username}}
<div class="top" v-if="item.admin_top===1">
<img class="BanSelected" src="@/assets/image/dingzi.png" alt="">
置顶</div>
</div>
<div class="create-at" @click="$router.push('/community/article-detail?id='+item.id)">
{{item.time}}
</div>
</div>
</div>
<div class="right" v-if="$store.getters.id!=item.user_id">
<div @click="show_done=!show_done">
<img src="@/assets/image/dosome.png" alt="" class="BanSelected">
</div>
<div class="do-tiezi" v-if="show_done">
<div class="do" @click="$router.push('/chat/chat-page/'+item.user_id+'?name='+item.username)">
私聊
</div>
<div @click="like_user()" class="do" v-if="item.is_like==1">
取消关注
</div>
<div @click="like_user()" class="do" v-else>
关注
</div>
<div class="do" @click="$router.push('/community/report?article_id='+item.id)">
举报
</div>
</div>
</div>
</div>
<div class="dynamic-content">
<!-- <div class="dynamic-text" @click="$router.push('/community/article-detail?id='+item.id)">{{item.content}}</div> -->
<template v-if="maxLines=0">
<div ref="desc" :class="showExchangeButton==true?'dynamic-text':'dynamic-texts'">{{item.content}}
<div v-if="showExchangeButton" class="more" @click="getMore(item.id)">展开全文</div>
</div>
</template>
<TextOverflow class="dynamic-text" :text="item.content" :width="400" :maxLines="5">
<template v-slot:default="{ clickToggle, expanded }">
<button @click="clickToggle" class="btn">
{{ expanded ? "收起" : "展开" }}
</button>
</template>
</TextOverflow>
<div class="video-wrap" v-if="item.video">
<Video :videoUrl="item.video">
</Video>
</div>
<div @click="$router.push('/community/article-detail?id='+item.id)">
<div class="dynamic-imglist-1" v-if="item.img.length==1">
<van-image class="dynamic-img-1" fit="cover" lazy-load :src="item.img[0]" />
</div>
<div class="dynamic-imglist-2" v-if="item.img.length==2">
<van-image class="dynamic-img-2" fit="cover" lazy-load :src="item.img[0]" />
<van-image class="dynamic-img-2" fit="cover" lazy-load :src="item.img[1]" />
</div>
<div class="dynamic-imglist-3" v-if="item.img.length>2">
<img v-if="item.img.length>3" class="img-more" src="@/assets/image/img4.png" alt="">
<van-image class="dynamic-img-3-1" fit="cover" lazy-load :src="item.img[0]" />
<div class="right">
<van-image class="dynamic-img-3-2" fit="cover" lazy-load :src="item.img[1]" />
<van-image class="dynamic-img-3-2" fit="cover" lazy-load :src="item.img[2]" />
</div>
</div>
</div>
<div class="tagList">
<div class="user-tag delete-able" v-for="(item,id) in item.label" :key="id">
#{{item}}
</div>
</div>
</div>
<div class="dynamic-bottom">
<div class="bottom-left">
<img v-if="item.address" src="@/assets/image/location.png" alt="">
{{item.address}}
</div>
<div class="bottom-right">
<div class="do-wrap" @click="toShare()">
<img src="@/assets/image/share-tiezi.png" alt="" class="BanSelected">
{{item.share}}
</div>
<div class="do-wrap" @click="$router.push('/community/article-detail?id='+item.id)">
<img src="@/assets/image/commit-tiezi.png" alt="" class="BanSelected">
{{item.comments_count}}
</div>
<div class="do-wrap" @click="like()">
<img v-if="item.is_like_article==0" src="@/assets/image/like-tiezi-default.png" alt="" class="BanSelected">
<img v-else src="@/assets/image/like-tiezi-active.png" alt="" class="BanSelected">
{{item.likes_count}}
</div>
</div>
</div>
</div>
</template>
<script>
import { likeArticle, likeUser, shareArticle } from "@/api/community";
import moment from "moment";
import wx from "weixin-js-sdk";
import { getUserInfo, qrcode } from "@/api/profile";
import Video from "@/components/Video";
import TextOverflow from '@/components/Overflow'
export default {
data() {
return {
show_done: false,
showExchangeButton:false,
maxLines: 4,
};
},
name: "",
components: {
Video,
TextOverflow
},
props: {
item: {
type: Object,
required: true
}
},
created() {
this.time = this.getTime(this.item.created_at);
// console.log(this.time)
// let getList=this.item
},
computed: {},
mounted(){
this.init()
// const lastWidth = this.$refs.desc.getClientRects();
// console.log(lastWidth)
// console.log(this.$refs.desc.getClientRects())
},
methods: {
init(){
// let descHeight =parseInt(window
// .getComputedStyle(this.$refs.desc)
// .height.replace("px", ""))
// // console.log(typeof(descHeight))
// let add = parseFloat(this.getRem());
// // console.log(typeof(add))
// // console.log(descHeight)
// // console.log(5.25*add)
// // console.log(`-------------------`);
// // console.log(descHeight - 5.25 *add);
// if(descHeight > 5.25 * add){
// this.showExchangeButton = true;
// // console.log('超过4行')
// }
},
// console.log("descHeight:" + descHeight)
getRem() {
const defaultRem = 16;
let winWidth = window.innerWidth;
let rem = (winWidth / 375) * defaultRem;
return rem;
},
getMore(id){
this.showExchangeButton=!this.showExchangeButton
// console.log(id)
// console.log(this.$refs.desc.getClientRects())
},
toShare() {
this.loading = this.$loading();
shareArticle({
id: this.$route.query.id
});
qrcode({
url: "/community/article-detail?id=" + this.item.id
}).then(res => {
this.loading.hidden();
if (res.code === 200) {
wx.miniProgram.navigateTo({
url: `/pages/case-share/case-share?params=${encodeURIComponent(
JSON.stringify({
width: "622rpx",
height: "826rpx",
views: [
{
type: "image",
url:
"https://zhixiu-picture.oss-cn-shanghai.aliyuncs.com/40bdfcc9ca8b9414b41801fd2a622a82086ae107.png",
css: {
top: "0rpx",
left: "0rpx",
width: "626rpx",
height: "826rpx"
}
},
{
type: "image",
url: res.data.url,
css: {
top: "534rpx",
left: "198rpx",
width: "232rpx",
height: "232rpx",
borderRadius: "50%"
}
},
{
type: "image",
url: this.item.avatar,
css: {
top: "60rpx",
left: "248rpx",
width: "130rpx",
height: "130rpx",
borderRadius: "50%",
borderWidth: "4rpx",
borderColor: "#fff",
borderStyle: "solid"
}
},
{
type: "text",
text: this.item.content.slice(0, 45) + "...",
css: {
fontSize: "28rpx",
fontweight: 400,
color: "#FFFFFF",
top: "238rpx",
left: "70rpx",
width: "486rpx",
lineHeight: "40rpx",
fontFamily: "PingFangSC-Regular,PingFang SC"
}
}
]
})
)}`
});
}
});
},
like_user() {
likeUser({
id: this.item.user_id
}).then(res => {
if (res.code == 200) {
this.show_done = false;
this.item.is_like = this.item.is_like == 0 ? 1 : 0;
}
});
},
like() {
likeArticle({ id: this.item.id }).then(res => {
if (res.code === 200) {
this.item.is_like_article = this.item.is_like_article == 0 ? 1 : 0;
if (this.item.is_like_article == 0) {
this.item.likes_count--;
} else {
this.item.likes_count++;
}
}
});
},
getTime(time) {
moment.locale("zh-cn");
}
},
watch:{
}
};
</script>
<style lang="scss" scoped>
.dynamic-wrap {
margin-bottom: 0.76rem;
.dynamic-bottom {
padding-left:1rem;
display: flex;
justify-content: space-between;
.bottom-right {
display: flex;
.do-wrap {
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #999999;
display: flex;
align-items: center;
margin-right: 0.42rem;
&:last-child {
margin-right: 0;
}
img {
width: 0.36rem;
margin-right: 0.1rem;
}
}
}
.bottom-left {
display: flex;
align-items: center;
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #999999;
img {
width: 0.22rem;
height: 0.24rem;
margin-right: 0.16rem;
}
}
}
.dynamic-content {
// text-align: justify;
position: relative;
margin: 0.28rem 0 0.34rem;
padding-left: 1rem;
padding-right: 0.3rem;
.dynamic-imglist-2 {
display: flex;
justify-content: space-between;
}
.dynamic-imglist-3 {
display: flex;
justify-content: space-between;
position: relative;
.img-more {
position: absolute;
bottom: 0.1rem;
right: 0.1rem;
width: 0.86rem;
z-index: 99;
height: 0.3rem;
}
.right {
display: flex;
flex-direction: column;
justify-content: space-between;
}
}
.dynamic-text {
font-size: 0.28rem;
font-family: PingFang-SC-Regular, PingFang-SC;
font-weight: 400;
color: #333333;
margin-bottom: 0.2rem;
white-space: pre-wrap;
// overflow: hidden;
// text-overflow: ellipsis;
// display: -webkit-box;
// -webkit-line-clamp: 4;
// -webkit-box-orient: vertical;
// word-break: break-word;
// position: relative;
// margin: 0;
// text-indent: -12em;
// padding-right: 4em;
// width: 80%;
// height: 10%;
// width: 80%;
}
// .dynamic-text:after{
// // position:absolute; left:0; bottom:0; width:100%; height:1px; border-bottom:1px solid #000; content:"";
// }
// .dynamic-texts {
// font-size: 0.28rem;
// font-family: PingFang-SC-Regular, PingFang-SC;
// font-weight: 400;
// color: #333333;
// // margin-bottom: 0.2rem;
// margin-bottom: 0.2rem;
// white-space: pre-wrap;
// // overflow: hidden;
// // text-overflow: ellipsis;
// // display: -webkit-box;
// // -webkit-line-clamp: 4;
// // -webkit-box-orient: vertical;
// }
.btn{
font-size: 0.28rem;
font-family: PingFang-SC-Regular, PingFang-SC;
color: #1678ff;
}
.dynamic-img-1 {
width: 2.72rem;
height: 3.81rem;
border-radius: 0.14rem;
overflow: hidden;
}
.dynamic-img-2 {
width: 2.72rem;
height: 2.72rem;
border-radius: 0.14rem;
overflow: hidden;
}
.dynamic-img-3-1 {
width: 3.68rem;
height: 3.59rem;
border-radius: 0.14rem;
overflow: hidden;
}
.dynamic-img-3-2 {
width: 1.74rem;
height: 1.74rem;
border-radius: 0.14rem;
overflow: hidden;
}
}
.dynamic-top {
display: flex;
justify-content: space-between;
.right {
position: relative;
margin-top: 0.1rem;
.do-tiezi {
position: absolute;
right: 0;
top: 0.42rem;
z-index: 9999999999;
width: 1.34rem;
height: 1.74rem;
background: #ffffff;
box-shadow: 0rem 0.04rem 0.08rem 0rem rgba(0, 0, 0, 0.1);
border-radius: 0.16rem;
border: 0.01rem solid rgba(0, 0, 0, 0.04);
display: flex;
flex-direction: column;
.do {
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #333333;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
}
img {
width: 0.34rem;
height: auto;
}
}
.left {
display: flex;
.avatar {
width: 0.8rem;
height: 0.8rem;
}
.dynamic-by {
margin-left: 0.24rem;
.username {
font-size: 0.32rem;
font-family: PingFangSC-Medium, PingFang SC;
font-weight: 500;
color: #666666;
margin-bottom: 0.02rem;
display: flex;
align-items: center;
.top {
margin-left: 0.2rem;
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #5599f6;
background: #f5f9fe;
display: flex;
align-items: center;
width: 1.06rem;
justify-content: center;
border-radius: 0.19rem;
img {
width: 0.16rem;
height: 0.2rem;
margin-right: 0.08rem;
}
}
}
.create-at {
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #999999;
}
}
}
}
.tagList {
display: flex;
margin-top: 0.32rem;
overflow-x: auto;
.user-tag {
flex-shrink: 0;
}
}
.delete-able {
background: rgba(51, 132, 245, 0.1);
position: relative;
font-size: 0.24rem;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #1678ff;
margin-right: 0.2rem;
margin-bottom: 0.2rem;
}
}
.BanSelected{
pointer-events: none;
-webkit-user-select: none;
-moz-user-select: none;
-webkit-user-select:none;
-o-user-select:none;
user-select:none;
}
</style>