使用CSS來(lái)顯示文字的溢出,例如....(省略號(hào)),並使用title屬性在懸停時(shí)顯示完整內(nèi)容,類(lèi)似彈出視窗
<script setup> import { ref } from 'vue' const msg = ref('Hello World! too much content in this text field component to display') </script> <template> <h1 :title=msg>{{ msg }}</h1> <input v-model="msg"> </template> <style> h1{ max-width: 15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style>
透過(guò)修改您的程式碼,我成功地比較了文字方塊的clientWidth
和scrollWidth
。
1- 消除了field
引用。
2- 為v-text-field
新增了一個(gè)id。
3- 新增了一個(gè)check
函數(shù),並在watch
回呼函數(shù)中呼叫它。
4- 在check
函數(shù)內(nèi)部,我檢查了輸入框的clientWidth
和scrollWidth
。
5- 為了在初始載入時(shí)運(yùn)行它,我將msg
賦值為空字串,並在腳本底部將其更改為原始字串。
在此處查看:這裡
#<script setup> import { ref, watch } from 'vue' const msg = ref("") const isCuttingOff = ref(false) function check() { const elm = document.querySelector('#txt') isCuttingOff.value = elm.clientWidth < elm.scrollWidth; // todo : custom tooltip or any other solution for long texts } watch( msg, (currentMsg, oldMessage, callback) => { callback(check) }, { immediate: true } ) msg.value = 'Hello World! too much content in this text cfield component to display.' </script> <script></script> <template> <v-app> <div class="text-h3">Is cutting off: {{ isCuttingOff }}</div> <v-container class="w-25"> <v-text-field id="txt" v-model="msg" /> </v-container> </v-app> </template>