HTML實(shí)現(xiàn)移動(dòng)端固定懸浮半透明搜索框
Question. 問題
在移動(dòng)端商城系統(tǒng)中,我們常??吹轿挥陧?yè)面頂部有一個(gè)搜索框,這類搜索框博主比較喜歡的是固定在頁(yè)面頂部,半透明懸浮,能依稀看見部分輪播圖的形式。
要制作這樣的搜索框,技術(shù)關(guān)鍵在于:
- fixed 搜索框定位
- opacity 設(shè)置透明度
Solution. 解決
首先我們定義一個(gè) html 片段:
<!-- 搜索框 -->
<header class="bar">
<form name="search" class="search" id="search" action="">
<div class="search-row">
<input type="search" name="word" id="word">
<span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
</div>
</form>
</header>
<!-- 一個(gè)背景圖 實(shí)際上這里往往是輪播圖 -->
<div class="background">
<img src="bg.jpg">
</div>
header 標(biāo)簽為搜索框,下面的 div 為一個(gè)背景圖。
同時(shí)附上 CSS 樣式:
<style type="text/css">
body {
margin: 0; padding: 0;
font-size: 14px; font-family: "microsoft yahei",'Arial', 'Verdana','Helvetica', sans-serif;
}
.bar {
position: fixed; top: 0; left: 0; right: 0; /* 決定了搜索框置頂 */
height: 44px; padding: 0 10px;
background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */
z-index: 10;
}
.bar form {
display: block; padding: 0;margin: 0;
}
.search-row {
position: relative;
height: 30px; padding: 7px 0;
}
.search-row input[type=search] {
position: absolute; top: 7px;
height: 30px; line-height: 21px; width: 100%; padding: 10px 15px 10px 30px;
border: 0; border-radius: 6px; outline: 0; background-color: rgba(0,0,0,0.1);
font-size: 16px; text-align: center;
z-index: 100;
}
.search-row .placeholder {
position: absolute; top: 2px; left: 0; right: 0;
display: inline-block; height: 34px; line-height: 34px;
border: 0; border-radius: 6px;
font-size: 16px; text-align: center; color: #999;
z-index: 1;
}
.search-row .placeholder .iconfont {
display: inline-block; width: 19px; line-height: 24px; padding: 10px 0;
font-size: 21px; color: #666;
}
.search-row .placeholder .text {
line-height: 40px;
vertical-align: top;
}
.background img {
width: 100%;
}
.active:before {
position: absolute; top: 11px; left: 5px; right: auto;
display: block; margin-right: 0;
font-size: 21px;
}
.active input[type=search] {
text-align: left
}
.active .placeholder{
display: none
}
</style>
很長(zhǎng)的一段 CSS 樣式,但是其核心就兩句話position: fixed; /* 決定了搜索框置頂 */ 和 background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */,其他的樣式均為了頁(yè)面的排版,排版的細(xì)節(jié)需要各位讀者自己寫一遍理解,過程可能需要花費(fèi)點(diǎn)時(shí)間。
這樣我們就完成了一個(gè)靜態(tài)的搜索框:
備注:這里的搜索圖標(biāo)使用了 iconfont,讀者可自行到 iconfont矢量圖標(biāo)庫(kù) 下載。
至此,我們還需要通過 JS 實(shí)現(xiàn)一些動(dòng)效:
用于實(shí)現(xiàn)用戶切換輸入時(shí)「搜索」位置圖標(biāo)的切換,原理很簡(jiǎn)單,增加和移除 class 類,這些類定義了樣式。
.active:before {
position: absolute; top: 11px; left: 5px; right: auto;
display: block; margin-right: 0;
font-size: 21px;
}
.active input[type=search] {
text-align: left
}
.active .placeholder{
display: none
}
<script type="text/javascript">
/* 輸入框獲取到焦點(diǎn) 表示用戶正在輸入 */
$("#word").focusin(function() {
$(".search-row").addClass("active iconfont icon-sousuo");
});
/* 輸入框失去焦點(diǎn) 表示用戶輸入完畢 */
$("#word").focusout(function() {
/* 判斷用戶是否有內(nèi)容輸入 */
if ($(this).val()=="") {
/* 沒有內(nèi)容輸入 改變樣式 */
$(".search-row").removeClass("active iconfont icon-sousuo");
} else {
/* 有內(nèi)容輸入 保持樣式 并提交表單 */
$("#search").submit();
}
});
</script>
備注:這里需要引入 jQuery,千萬別忘了!
Extension. 擴(kuò)展
完整 html 代碼:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<link rel="stylesheet" type="text/css" href="iconfont/iconfont.css">
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<style type="text/css">
body {
margin: 0; padding: 0;
font-size: 14px; font-family: "microsoft yahei",'Arial', 'Verdana','Helvetica', sans-serif;
}
.bar {
position: fixed; top: 0; left: 0; right: 0; /* 決定了搜索框置頂 */
height: 44px; padding: 0 10px;
background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */
z-index: 10;
}
.bar form {
display: block; padding: 0;margin: 0;
}
.search-row {
position: relative;
height: 30px; padding: 7px 0;
}
.search-row input[type=search] {
position: absolute; top: 7px;
height: 30px; line-height: 21px; width: 100%; padding: 10px 15px 10px 30px;
border: 0; border-radius: 6px; outline: 0; background-color: rgba(0,0,0,0.1);
font-size: 16px; text-align: center;
z-index: 100;
}
.search-row .placeholder {
position: absolute; top: 2px; left: 0; right: 0;
display: inline-block; height: 34px; line-height: 34px;
border: 0; border-radius: 6px;
font-size: 16px; text-align: center; color: #999;
z-index: 1;
}
.search-row .placeholder .iconfont {
display: inline-block; width: 19px; line-height: 24px; padding: 10px 0;
font-size: 21px; color: #666;
}
.search-row .placeholder .text {
line-height: 40px;
vertical-align: top;
}
.background img {
width: 100%;
}
.active:before {
position: absolute; top: 11px; left: 5px; right: auto;
display: block; margin-right: 0;
font-size: 21px;
}
.active input[type=search] {
text-align: left
}
.active .placeholder{
display: none
}
</style>
</head>
<body>
<!-- 搜索框 -->
<header class="bar">
<form name="search" class="search" id="search" action="">
<div class="search-row">
<input type="search" name="word" id="word">
<span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
</div>
</form>
</header>
<!-- 一個(gè)背景圖 實(shí)際上這里往往是輪播圖 -->
<div class="background">
<img src="bg.jpg">
</div>
</body>
<script type="text/javascript">
/* 輸入框獲取到焦點(diǎn) 表示用戶正在輸入 */
$("#word").focusin(function() {
$(".search-row").addClass("active iconfont icon-sousuo");
});
/* 輸入框失去焦點(diǎn) 表示用戶輸入完畢 */
$("#word").focusout(function() {
/* 判斷用戶是否有內(nèi)容輸入 */
if ($(this).val()=="") {
/* 沒有內(nèi)容輸入 改變樣式 */
$(".search-row").removeClass("active iconfont icon-sousuo");
} else {
/* 有內(nèi)容輸入 保持樣式 并提交表單 */
$("#search").submit();
}
});
</script>
</html>
總結(jié)
以上所述是小編給大家介紹的HTML實(shí)現(xiàn)移動(dòng)端固定懸浮半透明搜索框,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
欄 目:CSS/HTML
下一篇:HTML中的5種空格各表示的意義
本文標(biāo)題:HTML實(shí)現(xiàn)移動(dòng)端固定懸浮半透明搜索框
本文地址:http://www.jygsgssxh.com/a1/CSS_HTML/9534.html
您可能感興趣的文章
- 04-02好看的字體樣式css,好看的字體樣式圖片
- 04-02html中加入css樣式的簡(jiǎn)單介紹
- 01-10通過html為FLASH加鏈接的實(shí)現(xiàn)代碼(div層)
- 01-10IE瀏覽器HTML Hack標(biāo)簽總結(jié)
- 01-10html用style添加屬性示例
- 01-10HTML中rel屬性分析
- 01-10HTML元素設(shè)置焦點(diǎn)的方法
- 01-10在html文件里include文件內(nèi)容的方法小結(jié)
- 01-10常用HTML meta 標(biāo)簽屬性(網(wǎng)站兼容與優(yōu)化需要)
- 01-10用HTML和CSS打造屬于自己的暖男“大白”


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02表格樣式css樣式,css樣式表單
- 04-02好看的字體樣式css,好看的字體樣式圖
- 04-02分頁(yè)樣式css,分頁(yè)樣式j(luò)q
- 04-02分頁(yè)樣式css,分頁(yè)樣式欄里用來定義首
- 04-02css樣式的引入,css樣式怎么引入
- 04-02css滾動(dòng)條樣式,css滾動(dòng)條的樣式
- 04-02css樣式引入方式有幾種,網(wǎng)頁(yè)引入css樣
- 04-02html中加入css樣式的簡(jiǎn)單介紹
- 04-02vue中的css樣式布局,vue添加css樣式
- 04-02內(nèi)嵌樣式css,內(nèi)嵌樣式表,內(nèi)部樣式表
隨機(jī)閱讀
- 01-10bat批處理徹底隱藏文件的方法(使用
- 08-05dedecms織夢(mèng)模板編輯文檔的同時(shí)自動(dòng)更
- 01-10vue實(shí)現(xiàn)分頁(yè)加載效果
- 08-05DEDECMS點(diǎn)擊主欄目默認(rèn)顯示第一個(gè)子欄
- 08-05dedecms織夢(mèng)模板全站調(diào)用收藏?cái)?shù)的方法
- 01-11常用的HTML富文本編譯器UEditor、CKEdi
- 01-10C++ 類訪問控制的條件總結(jié)
- 08-05織夢(mèng)dedecms首頁(yè)調(diào)用縮略圖為背景
- 01-10C語言打印楊輝三角示例匯總
- 01-10C++實(shí)現(xiàn)將數(shù)組中的值反轉(zhuǎn)


