阅读更多/隐藏按钮工作得很好,但我只是想显示它的情况下,有一个线夹是上级2,我只是没有线索如何解决它
<template>
<div>
<div class="d-flex flex-row align-items-center">
<h4 class="card-title">{{ author }}</h4>
<h6 class="card-subtitle text-muted ml-3">
{{ timeAgo(createdAt) }} ago
</h6>
</div>
<p
class="multiline"
:style="{'-webkit-line-clamp': computedLineclamp }"
>
<b
class="timestamp"
v-if="timestamp !== null"
>
@{{ timeToHHMMSS(timestamp) }}
</b>
{{ content }}
</p>
<div>
<base-button
v-show="!readmore"
@click="changeLineclamp()"
link
class="text-white p-0"
size="sm"
>
Read more
</base-button>
<base-button
v-show="readmore"
@click="hideContent()"
link
class="text-white p-0"
size="sm"
>
Hide
</base-button>
</div>
</div>
</template>
<script>
// Internal modules
import { timeSince, toHHMMSS } from "@/plugins/timeControls.js";
export default {
name: "comment-item",
data() {
return {
lineclamp: 2,
readmore: false,
showButton: false,
isMultiline: false,
}
},
props: {
id: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
createdAt: {
type: Number,
required: true,
},
timestamp: {
type: Number,
required: true,
},
content: {
type: String,
required: true,
}
},
methods: {
hideButton(){
if (this.computedLineclamp) {
this.readmore;
}
},
changeLineclamp() {
this.lineclamp = 'initial';
this.readmore = true;
this.showButton = false;
},
hideContent() {
this.lineclamp = 2;
this.readmore = false
},
timeAgo(date) {
return timeSince(date);
},
timeToHHMMSS(time) {
return toHHMMSS(time);
},
},
computed: {
computedLineclamp() {
return this.lineclamp;
},
},
};
</script>
<style lang="scss" scoped>
@import "@/assets/sass/dashboard/custom/_variables.scss";
ul {
list-style-type: none;
padding: 0;
}
.button {
visibility: none;
}
.timestamp {
color: $primary;
}
.multiline {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
&.__lineclamp{
-webkit-line-clamp: 2;
}
&.__none{
-webkit-line-clamp: initial;
}
}
.button-visible {
display: block;
color: red;
&.__none {
display: none;
}
}
.hide {
line-clamp: none;
}
</style>
我尝试在内容中添加v-bind:class="{multiline__lineclamp:showButton==='button-visible',multiline__none:showButton==='button-visible'}”,但没有效果。我还尝试在脚本部分创建方法,但没有效果
<p
class="multiline"
:class="{
multiline__lineclamp:showButton === 'button-visible',
multiline__none:showButton === 'button-visible__none'
}"
:style="{ '-webkit-line-clamp': computedLineclamp }"
>
<b
class="timestamp"
v-if="timestamp !== null"
>
@{{ timeToHHMMSS(timestamp) }}
</b>
{{ content }}
</p>
1条答案
按热度按时间2skhul331#
正如您所说-"我只是想展示一下,以防有一个线夹优于2。"
我的理解是,您希望仅在文本夹大于2时显示
read_more/hide
按钮。为了做到这一点,你可以检查这两个条件-
这是一个工作演示,它将像这样工作-
1.默认情况下,
line-clamp: 2
属性将应用于文本。read_more/hide
按钮仅在段落内的文本超过两行时才可见,并且您可以显示/隐藏内容。1.演示中有三个按钮可以将文本更改为一行、两行和三行,因此您可以在它们之间切换并查看结果。
1.我创建了一个名为
hide_btns
的数据属性,用于在文本箝位小于2时隐藏两个按钮(read_more/hide)
。1.然后,我创建了一个名为
checkIfElClamped
的方法,该方法将检查文本钳位长度并相应地切换数据属性hide_btns
。1.我还使用了一个监视器钩子来监视文本,当文本发生变化时,它调用相同的方法
checkIfElClamped
来再次执行文本箝位操作。x一个一个一个一个x一个一个二个x