国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

HTML實現移動端固定懸浮半透明搜索框

2020-03-24 17:11:07
字體:
來源:轉載
供稿:網友
現在互聯網已經有成千上百個網站,然而網站少不了的一個功能就是搜索,我們可以看到很多網站的搜索框各有不同,在移動端也是如此。本文我們就和大家分享一種在移動端固定在頁面頂部,半透明懸浮,能依稀看見部分輪播圖形式的搜索框。

11.jpg

要制作這樣的搜索框,技術關鍵在于:

fixed 搜索框定位

opacity 設置透明度

Solution. 解決

首先我們定義一個 html 片段:

 !-- 搜索框 --  header >

header 標簽為搜索框,下面的 p 為一個背景圖。

同時附上 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 

很長的一段 CSS 樣式,但是其核心就兩句話position: fixed; /* 決定了搜索框置頂 */ 和 background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */,其他的樣式均為了頁面的排版,排版的細節需要各位讀者自己寫一遍理解,過程可能需要花費點時間。

這樣我們就完成了一個靜態的搜索框:

11.jpg

備注:這里的搜索圖標使用了 iconfont,讀者可自行到 iconfont矢量圖標庫 下載。

至此,我們還需要通過 JS 實現一些動效:

201710170918224.gif

用于實現用戶切換輸入時「搜索」位置圖標的切換,原理很簡單,增加和移除 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 /* 輸入框獲取到焦點 表示用戶正在輸入 */$( #word ).focusin(function() { $( .search-row ).addClass( active iconfont icon-sousuo /* 輸入框失去焦點 表示用戶輸入完畢 */$( #word ).focusout(function() { /* 判斷用戶是否有內容輸入 */ if ($(this).val()== ) { /* 沒有內容輸入 改變樣式 */ $( .search-row ).removeClass( active iconfont icon-sousuo  } else { /* 有內容輸入 保持樣式 并提交表單 */ $( #search ).submit(); /script 

備注:這里需要引入 jQuery,千萬別忘了!

Extension. 擴展

完整 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 >

以上內容就是HTML實現移動端固定懸浮半透明搜索框的教程,希望大家在開發中能幫助到大家。

相關推薦:

css制作好看的搜索框

如何用Js實現百度搜索框提示功能

分享8款CSS3搜索框

以上就是HTML實現移動端固定懸浮半透明搜索框的詳細內容,html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 论坛| 奉新县| 五莲县| 秭归县| 白山市| 商丘市| 多伦县| 闵行区| 武威市| 靖远县| 临江市| 长丰县| 莲花县| 泸州市| 吉木乃县| 龙游县| 江津市| 大埔区| 陈巴尔虎旗| 广东省| 舟山市| 台中市| 江油市| 平度市| 安阳市| 广州市| 连城县| 新竹县| 余姚市| 保定市| 四会市| 涿州市| 慈溪市| 涟源市| 县级市| 苗栗市| 射阳县| 潞西市| 德格县| 新源县| 慈溪市|