前端Bug解決指南

<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
<meta content="email=no" name="format-detection" />
<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL" />


<meta name="HandheldFriendly" content="true">
<meta name="MobileOptimized" content="320">
<meta name="screen-orientation" content="portrait">
<meta name="x5-orientation" content="portrait">
<meta name="full-screen" content="yes">
<meta name="x5-fullscreen" content="true">
<meta name="browsermode" content="application">
<meta name="x5-page-mode" content="app">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />

// css

<style>
    input:focus::-webkit-input-placeholder {
        opacity: 0;
    }
</style>

//js rem布局代碼 font-size:恒定值為100px

<script>
    (function(doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function() {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if (clientWidth >= 640) {
                    docEl.style.fontSize = '100px';
                } else {
                    docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
                }
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
</script>

1,如何清除圖片下方出現幾像素的空白間隙?
方法1:
img{display:block;}
方法2:
img{vertical-align:top;}
除了top值,還可以設置為text-top | middle | bottom | text-bottom,甚至特定的<length>和<percentage>值都可以
方法3:

test{font-size:0;line-height:0;}

test為img的父元素

2,如何讓文本垂直對齊文本輸入框?
input{vertical-align:middle;}

3,為什么Standard mode下IE無法設置滾動條的顏色?
html{
scrollbar-3dlight-color:#999;
scrollbar-darkshadow-color:#999;
scrollbar-highlight-color:#fff;
scrollbar-shadow-color:#eee;
scrollbar-arrow-color:#000;
scrollbar-face-color:#ddd;
scrollbar-track-color:#eee;
scrollbar-base-color:#ddd;
}
將原來設置在body上的滾動條顏色樣式定義到html標簽選擇符上即可

4,如何使文本溢出邊界顯示為省略號?
方法(此方法Firefox5.0尚不支持):

test{width:150px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}

首先需設置將文本強制在一行內顯示,然后將溢出的文本通過overflow:hidden截斷,并以text-overflow:ellipsis方式將截斷的文本顯示為省略號。

5,如何清除浮動?
方法1:

test{clear:both;}

test為浮動元素的下一個兄弟元素

方法2:

test{display:block;zoom:1;overflow:hidden;}

test為浮動元素的父元素。zoom:1也可以替換為固定的width或height

方法3:

test{zoom:1;}

test:after{display:block;clear:both;visibility:hidden;height:0;content:'';}

test為浮動元素的父元素

6,如何讓已知高度的容器在頁面中水平垂直居中?

test{position:absolute;top:50%;left:50%;width:200px;height:200px;margin:-100px 0 0 -100px;}

7,如何讓未知尺寸的圖片在已知寬高的容器內水平垂直居中?
方法:

test{display:table-cell;display:block;position:relative;width:200px;height:200px;text-align:center;vertical-align:middle;}

test p{position:absolute;top:50%;*left:50%;margin:0;}

test p img{position:relative;top:-50%;*left:-50%;vertical-align:middle;}

test是img的祖父節(jié)點,p是img的父節(jié)點。Know More:未知尺寸的圖片如何水平垂直居中

8,如何讓某個元素充滿整個頁面?
方法:
html,body{height:100%;margin:0;}

test{height:100%;}

9,如何讓某個元素距離窗口上右下左4邊各10像素?
方法:
html,body{height:100%;margin:0;}
html{_padding:10px;}

test{position:absolute;top:10px;right:10px;bottom:10px;left:10px;_position:static;_height:100%;}

10,如何容器透明,內容不透明?
方法1:
.outer{width:200px;height:200px;background:#000;filter:alpha(opacity=20);opacity:.2;}
.inner{width:200px;height:200px;margin-top:-200px;}

<div class="outer"></div>
<div class="inner">我是不透明的內容</div>
原理是容器層與內容層并級,容器層設置透明度,內容層通過負margin或者position絕對定位等方式覆蓋到容器層上
方法2:
.outer{width:200px;height:200px;background:rgba(0,0,0,.2);background:#000\9;filter:alpha(opacity=20)\9;}
.outer .inner{position:relative\9;}

<div class="outer">
<div class="inner">我是不透明的內容</div>
</div>
高級瀏覽器直接使用rgba顏色值實現;IE瀏覽器在定義容器透明的同時,讓子節(jié)點相對定位,也可達到效果

11,如何做1像素細邊框的table?
方法1:

test{border-collapse:collapse;border:1px solid #ddd;}

test th,#test td{border:1px solid #ddd;}

<table id="test">
<tr><th>姓名</th><td>Joy Du</td></tr>
<tr><th>年齡</th><td>26</td></tr>
</table>
方法2:

test{border-spacing:1px;background:#ddd;}

test tr{background:#fff;}

<table id="test" cellspacing="1">
<tr><th>姓名</th><td>Joy Du</td></tr>
<tr><th>年齡</th><td>26</td></tr>
</table>
IE7及更早瀏覽器不支持border-spacing屬性,但是可以通過table的標簽屬性cellspacing來替代。

12,如何使頁面文本行距始終保持為n倍字體大小的基調?
方法:
body{line-height:n;}
注意,不要給n加單位。Know More:如何使頁面文本行距始終保持為n倍字體大小的基調

13,以圖換字的幾種方法及優(yōu)劣分析
思路1:使用text-indent的負值,將內容移出容器
.test1{width:200px;height:50px;text-indent:-9999px;background:#eee url(*.png) no-repeat;}

<div class="test">以圖換字之內容負縮進法</div>
該方法優(yōu)點在于結構簡潔,不理想的地方:1.由于使用場景不同,負縮進的值可能會不一樣,不易抽象成公用樣式;2.當該元素為鏈接時,在非IE下虛線框將變得不完整;3.如果該元素被定義為內聯級或者內聯塊級,不同瀏覽器下會有較多的差異
思路2:使用display:none或visibility:hidden將內容隱藏;
.test{width:200px;height:50px;background:#eee url(*.png) no-repeat;}
.test span{visibility:hidden;}

<div class="test"><span>以圖換字之內容隱藏法</span></div>
該方法優(yōu)點在于兼容性強并且容易抽象成公用樣式,缺點在于結構較復雜
思路3:使用padding或者line-height將內容擠出容器之外;
.test{overflow:hidden;width:200px;height:0;padding-top:50px;background:#eee url(.png) no-repeat;}
.test{overflow:hidden;width:200px;height:50px;line-height:50;background:#eee url(
.jpg) no-repeat;}

<div class="test">以圖換字之內容排擠法</div>
該方法優(yōu)點在于結構簡潔,缺點在于:1.由于使用場景不同,padding或line-height的值可能會不一樣,不易抽象成公用樣式;2.要兼容IE5.5及更早瀏覽器還得hack
思路4:使用超小字體和文本全透明法;
.test{overflow:hidden;width:200px;height:50px;font-size:0;line-height:0;color:rgba(0,0,0,0);background:#eee url(*.png) no-repeat;}

<div class="test">以圖換字之超小字體+文本全透明法</div>
該方法結構簡單易用,推薦使用

14,為什么2個相鄰div的margin只有1個生效?
方法:
.box1{margin:10px 0;}
.box2{margin:20px 0;}

<div class="box1">box1</div>
<div class="box2">box2</div>
本例中box1的底部margin為10px,box2的頂部margin為20px,但表現在頁面上2者之間的間隔為20px,而不是預想中的10+20px=30px,結果是選擇2者之間最大的那個margin,我們把這種機制稱之為“外邊距合并”;外邊距合并不僅僅出現在相鄰的元素間,父子間同樣會出現。
簡單列舉幾點注意事項:
 外邊距合并只出現在塊級元素上;
 浮動元素不會和相鄰的元素產生外邊距合并;
 絕對定位元素不會和相鄰的元素產生外邊距合并;
 內聯塊級元素間不會產生外邊距合并;
 根元素間不會不會產生外邊距合并(如html與body間);
 設置了屬性overflow且值不為visible的塊級元素不會與它的子元素發(fā)生外邊距合并;

15,如何在文本框中禁用中文輸入法?
方法:
input,textarea{ime-mode:disabled;}
ime-mode為非標準屬性,寫該文檔時只有IE和Firefox支持

16,如何解決Chrome在應用transition時頁面閃動的問題?
-webkit-transform-style:preserve-3d;或-webkit-backface-visibility:hidden;

17,<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />這行代碼是永遠以最新的IE版本模式來顯示網頁,使IE支持HTML5。

18 IE8下 console.log必須在測試完畢后全部注釋掉

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. tab列表折疊效果 html: 能源系統事業(yè)部 崗位名稱: 工作地點 崗位名...
    lilyping閱讀 1,891評論 0 1
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,796評論 1 92
  • 說明:本文檔兼容性測試基礎環(huán)境為:windows系統;IE6-10, Firefox4-17, Chrome16-...
    假行僧396741閱讀 363評論 0 3
  • 完成啦
    一株草的名字閱讀 221評論 0 2
  • 感謝神,感恩克通弟兄從香港發(fā)來的信息,照片,克通弟兄追求,上進,渴慕神,你是好樣的,感恩安哥請我吃西瓜,快樂的一天!
    張世超閱讀 120評論 0 0