overflow、box-shadow、鼠标样式、换行

本文深入讲解CSS中的overflow属性,阴影效果,鼠标样式调整,input和textarea的样式控制,以及文本的换行和显示策略,帮助读者掌握更高级的网页布局和样式设计技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、overflow 属性

属性描述
overflow:visible属性的默认值,超出显示
overflow:auto自动的,超出就显示滚动条,不超出就不显示
overflow:scroll一直会显示滚动条
overflow:hidden超出自动隐藏

2、阴影

box-shadow:水平阴影 垂直阴影 模糊距离  阴影尺寸 阴影颜色  内/外阴影;

3、鼠标样式

分类描述例子
cursor:default默认 没有样式在这里插入图片描述
cursor:pointer鼠标移动上去会显示小手在这里插入图片描述
cursor:move鼠标移动上去会显示移动的图标在这里插入图片描述
cursor:text鼠标移动上去会显示文本的图标在这里插入图片描述

3、去掉input的外轮廓线

<input type="text" style="outline:0"/>

4、防止拖拽文本域resize

<textarea style="resize:none;"/>

5、换行

【1】自动换行

属性描述
word-break:break-all允许单词拆开显示
word-break:keep-all不允许拆开单词,只能在半角空格或连字符处换行
word-break:normal使用浏览器默认的换行规则

【2】强制一行显示
white-space设置或检索对象文本显示方式,通常我们强制一行显示内容
normal:默认处理方式
nowrap:强制在同一行内显示所有文本,知道文本结束或者遇到br标签换行
【3】超出显示…

 .text{
 	width:120px;
 	height:20px;
 	white-space:nowrap;/* 首先要添加这一行代码,强制一行显示*/
 	overflow:hidden;/* 然后超出隐藏*/
 	text-overflow:ellipsis;/*超出部分用省略号代替 */
 }
<template> <div class="container" :style="w_h" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave"> <pre class="ellipsis-pre" ref="preElement" v-html="content" :style="style"></pre> <span v-if="show"> ... </span> <!-- <NCard v-if="showPopup" class="popup" :style="popupStyle"> <pre v-html="content"></pre> </NCard> --> <div v-if="showPopup" class="popup" :style="popupStyle"> <pre v-html="content"></pre> </div> </div> </template> <script setup lang="ts"> import { NCard, NText } from 'naive-ui'; import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue'; const props = defineProps<{ content: string; width?: string; height?: string; style?: {}; }>(); const preElement = ref<HTMLElement | null>(null); const showPopup = ref(false); const style = ref(props.style ?? {}); const popupStyle = ref(); const show = ref(false) const contentLen = ref(0) const w_h = ref({ width: props.width, height: props.height }); let intervalId: number | null = null; const handleMouseEnter = (event: MouseEvent) => { if (show.value) { if (intervalId) window.clearInterval(intervalId); intervalId = window.setTimeout(() => { if (preElement.value) { let w = window.innerWidth let h = window.innerHeight console.log(w / 2, h / 2) const rect = preElement.value.getBoundingClientRect(); if (rect.y > h / 2) { popupStyle.value = { bottom: '0px', right: '0px', minWidth: props.width, minHeight: props.height } } else { popupStyle.value = { top: '0px', right: '0px', minWidth: props.width, minHeight: props.height } } console.log(rect) // popupStyle.value = { // top: `${rect.bottom + window.scrollY}px`, // left: `${rect.left + window.scrollX}px` }; showPopup.value = true; }, 1000); } }; const handleMouseLeave = () => { if (intervalId) window.clearInterval(intervalId); showPopup.value = false; }; const checkOverflow = () => { if (preElement.value) { const element = preElement.value; const overflowStyle = window.getComputedStyle(element).overflow; if (overflowStyle === 'hidden') { console.log('overflow: hidden 生效了'); } else { console.log('overflow: hidden 没有生效'); } // 验证内容是否溢出 const isOverflowingVertically = element.scrollHeight > element.clientHeight; const isOverflowingHorizontally = element.scrollWidth > element.clientWidth; if (isOverflowingVertically || isOverflowingHorizontally) { console.log('内容溢出了容器'); show.value = true } else { console.log('内容没有溢出容器'); } } else { console.log('元素未被正确引用'); } }; onMounted(() => { if (preElement.value) { preElement.value.style.width = props.width || '300px'; preElement.value.style.height = props.height || '100px'; } checkOverflow() }); onUnmounted(() => { if (intervalId) window.clearInterval(intervalId); }); // 使用 watch 监听 props.content 的变化 watch(() => props.content, (newContent) => { if (newContent) { contentLen.value = newContent.split('\n').length; } else { contentLen.value = 0; } }, { immediate: true }); </script> <style scoped lang="scss"> .container { position: relative; place-items: center; /* 垂直和水平居中 */ width: 100%; height: 100%; } .ellipsis-pre { display: flex; align-items: center; //white-space: pre-wrap; // 保留空白符序列,但是正常地进行换行 word-wrap: break-word; // 在长单词或 URL 地址内部进行换行 overflow: hidden; // 隐藏溢出的内容 text-overflow: ellipsis; // 显示省略号 //border: 1px solid #000; padding: 2px; box-sizing: border-box; width: 100%; height: 100%; text-align: left; position: absolute; line-height: 1.1; } .popup { position: absolute; background-color: #fff; border: 1px solid #ebe7e7; padding: 10px; box-shadow: 1 2px 10px rgba(0, 0, 0, 0.1); z-index: 1001; max-width: 50vw; max-height: 30vh; overflow: auto; white-space: pre-wrap; // 保留空白符序列,但是正常地进行换行 word-wrap: break-word; // 在长单词或 URL 地址内部进行换行 } span { position: absolute; right: 0px; top: 100; bottom: 0; z-index: 1000; color: rgba(71, 150, 241, 0.726); } </style> 优化这个组件 美化弹窗样式
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值