display:inline-block终极解决方案

本文详细探讨了display:inline-block的使用方法及其在不同浏览器下的兼容性问题,特别指出间隙问题产生的原因及解决方法,包括如何使用CSSHack使display:inline-block在IE6/7下生效,以及如何避免内联元素之间的间隙。
对于display:inline-block,做项目的时候经常用到,经常出问题,所以在今天,终于下定决心要好好整理下。
众所周知:inline-block的作用是将对象呈递为内联对象,但是对象的内容作为块对象呈递。旁边的内联对象会被呈递在同一行内,允许空格。这就给我们带来很大的便利,比如不用浮动,就可以将元素呈递为内联样式,而且还可以直接给元素设置宽度、高度、边距等等。俗话说,有利就有弊,没有东西是完美的。inline-block在给我们带来好处的同时,也带给了我们很多麻烦。就比如前端要考虑的兼容问题。
一、兼容性
IE6/IE7下对display:inline-block的支持性不好。
1、inline元素的display属性设置为inline-block时,所有的浏览器都支持;
2、block元素的display属性设置为inline-block时,IE6/IE7浏览器是不支持的;
就如下面的代码:
html:
<ul class="test">
        <li>放假</li>
        <li>放假</li>
        <li>放假</li>
        <li>放假</li>
        <li>放假</li>
        <li>放假</li>
    </ul>
css:
.test {width:200px;height:200px;background: #add8e6;padding:80px 40px 20px;}
.test li {display:inline-block;background:#f08080;border:1px solid #fafad2;width:50px;height:50px;line-height:50px;text-align:center;font-size:16px;font-family:'microsoft yahei';color:#fff;list-style-type: none;margin-top: -1px;}
在ff下是这样的:


在IE6/7下却是这样的:


哎,看到这一幕的时候,心情很忧伤呐。不过还好,我们可以用Css Hack方法让display:inline-block在IE6/7下生效。
在刚刚的代码后面加上*display:inline;*zoom:1;这两句就可以了。

二、出现间隙
查了资料,才知道display:inline-block在IE下仅仅是触发了layout,而它本是行布局,触发后,块元素依然还是行布局。所以需让块元素呈递为内联对象,display:inline则起到了这一作用(*号只被ie6、7所识别),然后再通过zoom:1触发layout。即可实现所谓的inline-block。原本以为事情就这样结束了,因为以前的项目都是要求inline-block后呈递的元素之间有边距,所以也没有考虑太多。但是今天做这个的时候,我发现了问题的所在。

左边这块和右边这块都被我设置为inline-block,但是悲剧出现了,中间有间隙!!!为什么会出现间隙呢?难道是本身就设置了边距?于是我又margin:0;padding:0;试了一下,没用!那就证明不是margin和padding的问题。到底是什么呢?仔细想想,刚前面说了inline-block支持空格,于是恍然大悟,才发现,原来是里边的换行符、空格等在作祟,干掉它们,间隙就没有了。

关键的来了,font-size:0;加上这一句,间隙立马消失的无影无踪了。







三、总结

每一个看似简单的东西,都需要我们深入的去理解,去运用,然后再次理解、运用。特别是当我们遇到问题,或者是实际的效果跟我们预期的效果不一致时,不要先去想怎样解决它,要首先去想出现这个问题的原因。因为如果只是想着怎么去解决的话,有可能用错误的方式获得了正确的效果,这样你就离正确的东西越来越远了。如果遇到问题,就找出 出现这个问题的根本原因,那么就能对症下药,并很快的找到正确的解决方法。这样才能很快的进步。特别是在前端这一行,会出现很多的问题,以前我遇到问题总是只想着怎么去解决它,有的时候就用了错误的方法,但后来我才发现,结果有时候并没有过程重要,在前端这一行,很有可能用错误的方法却得到了正确的答案,但千万不要认为自己就正确了,有可能这个错会导致接下来的更多问题。归根结底,永远不要认为我们什么都懂了,或者什么东西太简单了,就不去研究。我们应该不断的保持探索的精神,在前端的道路上,扎实自己的基础,坚定自己的信念,奋斗下去,菜鸟终将变成大鸟!


<template> <zy-voice-recorder ref="voiceRecorder" @start="onRecordStart" @stop="onRecordStop" @error="onRecordError"> <view class="sound-container" v-show="showSoundCom" :style="{ 'background-image': `linear-gradient(179deg, rgba(255, 255, 255, 0) 0%, #ffffff ${soundTop}px)` }" > <view class="sound-content" :style="{ top: `${soundTop - 16}px` }" > <view class="sound-conten-wave"> <view class="sound-conten-wave-bg"> <view class="wave"> <div class="voice-container"> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> </div> </view> <view class="arrow-cion"></view> </view> <view class="text">松开结束语音转文字</view> </view> <view class="sound-content-icon"> <uni-icons v-show="soundTop != 0" type="yuyinzhong" color="#0F56D5" size="17" class="mic-icon" style="position: absolute; font-size: 17px; left: 70%; top: 50%; transform: translate(-50%, -50%)" /> <image class="img" lazy-load src="https://gateway-in.rosino.com/prod/biz-zy-gateway/admin-api/infra/file/22/get/xzzl/sound.png" mode="widthFix" /> </view> </view> </view> </zy-voice-recorder> </template> <script lang="ts" setup> interface Props { soundTop: number visible: boolean } const props = withDefaults(defineProps<Props>(), { visible: false, soundTop: 0 }) const $emit = defineEmits<{ (e: 'end', value: string): void }>() const showSoundCom = ref(false) const voiceRecorder = ref() // 录音开始回调 function onRecordStart(res: any) { console.log('录音开始', res) } // 录音结束回调 function onRecordStop(data: any) { console.log('录音结束', data) $emit('end', data.result) // 如果需要,可以在这里处理录音文件和识别结果 // const audioPath = data.tempFilePath; // const result = data.result; } // 录音错误回调 function onRecordError(res: any) { console.error('录音错误', res) } watch( () => props.visible, async val => { if (val) { showSoundCom.value = true // 等待 Vue 更新 DOM await nextTick() // 再等待一帧,确保样式和布局完成(约 16ms) setTimeout(() => { voiceRecorder.value?.startRecord() }, 16) } else { voiceRecorder.value?.stopRecord?.() showSoundCom.value = false } }, { immediate: false, deep: true } ) </script> <style scoped lang="scss"> .sound-container { top: 0; position: fixed; z-index: 99; height: 100vh; width: 100%; .sound-content { width: 100%; // position: absolute; display: flex; justify-content: center; align-items: center; position: relative; .sound-conten-wave { .sound-conten-wave-bg { display: flex; align-items: center; } .arrow-cion { border: 8px solid #5b8ff9; border-color: transparent transparent transparent #5b8ff9; } .wave { width: 190px; height: 60px; display: flex; align-items: center; background: #5b8ff9; border-radius: 4px; justify-content: center; .voice-container { display: flex; align-items: center; justify-content: center; height: 30px; gap: 2px; /* 条形间距 */ @keyframes wave { 0%, 100% { height: 15px; } /* 最矮状态 */ 50% { height: 8px; } /* 最高状态 */ } .bar { width: 2px; height: 15px; background-color: #fff; /* 微信绿色 */ border-radius: 3px; animation: wave 1.2s infinite ease-in-out; } .bar:nth-child(1) { animation-delay: 0s; } .bar:nth-child(2) { animation-delay: 0.2s; } .bar:nth-child(3) { animation-delay: 0.4s; } .bar:nth-child(4) { animation-delay: 0.6s; } .bar:nth-child(5) { animation-delay: 0.8s; } .bar:nth-child(6) { animation-delay: 0s; } .bar:nth-child(7) { animation-delay: 0.2s; } .bar:nth-child(8) { animation-delay: 0.4s; } .bar:nth-child(9) { animation-delay: 0.6s; } .bar:nth-child(10) { animation-delay: 0.8s; } .bar:nth-child(11) { animation-delay: 0s; } .bar:nth-child(12) { animation-delay: 0.2s; } .bar:nth-child(13) { animation-delay: 0.4s; } .bar:nth-child(14) { animation-delay: 0.6s; } .bar:nth-child(15) { animation-delay: 0.8s; } .bar:nth-child(16) { animation-delay: 0s; } .bar:nth-child(17) { animation-delay: 0.2s; } .bar:nth-child(18) { animation-delay: 0.4s; } .bar:nth-child(19) { animation-delay: 0.6s; } .bar:nth-child(20) { animation-delay: 0.8s; } .bar:nth-child(21) { animation-delay: 0s; } .bar:nth-child(22) { animation-delay: 0.2s; } .bar:nth-child(23) { animation-delay: 0.4s; } .bar:nth-child(24) { animation-delay: 0.6s; } .bar:nth-child(25) { animation-delay: 0.8s; } } } .text { font-size: 14px; padding: 12px 8px 0 0; color: #999999; text-align: center; } } .sound-content-icon { position: absolute; right: 0; top: -45px; width: 80px; .img { width: 100%; vertical-align: top; } } } } .mic-icon { position: absolute; font-size: 17px; left: 70%; top: 50%; transform: translate(-50%, -50%); } </style> icon还是会出现在点击后展示的图片的上边,然后闪现回到图片的中间
最新发布
11-18
<template> <zy-voice-recorder ref="voiceRecorder" @start="onRecordStart" @stop="onRecordStop" @error="onRecordError"> <view class="sound-container" v-show="showSoundCom" :style="{ 'background-image': `linear-gradient(179deg, rgba(255, 255, 255, 0) 0%, #ffffff ${soundTop}px)` }" > <view class="sound-content" :style="{ top: `${soundTop}px` }" > <view class="sound-conten-wave"> <view class="sound-conten-wave-bg"> <view class="wave"> <div class="voice-container"> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> </div> </view> <view class="arrow-cion"></view> </view> <view class="text">松开结束语音转文字</view> </view> <view class="sound-content-icon"> <uni-icons type="yuyinzhong" color="#0F56D5" size="17" style="position: absolute; font-size: 17px; left: 70%; top: 50%; transform: translate(-50%, -50%)" /> <image class="img" lazy-load src="https://gateway-in.rosino.com/prod/biz-zy-gateway/admin-api/infra/file/22/get/xzzl/sound.png" mode="widthFix" /> </view> </view> </view> </zy-voice-recorder> </template> <script lang="ts" setup> interface Props { soundTop: number visible: boolean } const props = withDefaults(defineProps<Props>(), { visible: false, soundTop: 0 }) const $emit = defineEmits<{ (e: 'end', value: string): void }>() const showSoundCom = ref(false) const voiceRecorder = ref() // 录音开始回调 function onRecordStart(res: any) { console.log('录音开始', res) } // 录音结束回调 function onRecordStop(data: any) { console.log('录音结束', data) $emit('end', data.result) // 如果需要,可以在这里处理录音文件和识别结果 // const audioPath = data.tempFilePath; // const result = data.result; } // 录音错误回调 function onRecordError(res: any) { console.error('录音错误', res) } watch( () => props.visible, val => { console.log(val, 'val') setTimeout(() => { showSoundCom.value = val if (val) { console.log(4444) voiceRecorder.value.startRecord() } else { console.log(3333) voiceRecorder.value.stopRecord() } }, 1000) }, { immediate: false, deep: true } ) </script> <style scoped lang="scss"> .sound-container { top: 0; position: fixed; z-index: 99; height: 100vh; width: 100%; .sound-content { width: 100%; position: absolute; display: flex; justify-content: center; align-items: center; .sound-conten-wave { .sound-conten-wave-bg { display: flex; align-items: center; } .arrow-cion { border: 8px solid #5b8ff9; border-color: transparent transparent transparent #5b8ff9; } .wave { width: 190px; height: 60px; display: flex; align-items: center; background: #5b8ff9; border-radius: 4px; justify-content: center; .voice-container { display: flex; align-items: center; justify-content: center; height: 30px; gap: 2px; /* 条形间距 */ @keyframes wave { 0%, 100% { height: 15px; } /* 最矮状态 */ 50% { height: 8px; } /* 最高状态 */ } .bar { width: 2px; height: 15px; background-color: #fff; /* 微信绿色 */ border-radius: 3px; animation: wave 1.2s infinite ease-in-out; } .bar:nth-child(1) { animation-delay: 0s; } .bar:nth-child(2) { animation-delay: 0.2s; } .bar:nth-child(3) { animation-delay: 0.4s; } .bar:nth-child(4) { animation-delay: 0.6s; } .bar:nth-child(5) { animation-delay: 0.8s; } .bar:nth-child(6) { animation-delay: 0s; } .bar:nth-child(7) { animation-delay: 0.2s; } .bar:nth-child(8) { animation-delay: 0.4s; } .bar:nth-child(9) { animation-delay: 0.6s; } .bar:nth-child(10) { animation-delay: 0.8s; } .bar:nth-child(11) { animation-delay: 0s; } .bar:nth-child(12) { animation-delay: 0.2s; } .bar:nth-child(13) { animation-delay: 0.4s; } .bar:nth-child(14) { animation-delay: 0.6s; } .bar:nth-child(15) { animation-delay: 0.8s; } .bar:nth-child(16) { animation-delay: 0s; } .bar:nth-child(17) { animation-delay: 0.2s; } .bar:nth-child(18) { animation-delay: 0.4s; } .bar:nth-child(19) { animation-delay: 0.6s; } .bar:nth-child(20) { animation-delay: 0.8s; } .bar:nth-child(21) { animation-delay: 0s; } .bar:nth-child(22) { animation-delay: 0.2s; } .bar:nth-child(23) { animation-delay: 0.4s; } .bar:nth-child(24) { animation-delay: 0.6s; } .bar:nth-child(25) { animation-delay: 0.8s; } } } .text { font-size: 14px; padding: 12px 8px 0 0; color: #999999; text-align: center; } } .sound-content-icon { position: absolute; right: 0; top: -45px; width: 80px; .img { width: 100%; vertical-align: top; } } } } </style>当前组件打开的候,icon会在展示的盒子出现前闪现出现在点击的区域上方,并在展示的盒子出现后回到盒子中心
11-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值