六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

html移動端固定懸浮半透明搜索框的完成方法說明

[摘要]Question. 問題在移動端商城系統中,我們常常看到位于頁面頂部有一個搜索框,這類搜索框博主比較喜歡的是固定在頁面頂部,半透明懸浮,能依稀看見部分輪播圖的形式。要制作這樣的搜索框,技術關鍵在于:fixed 搜索框定位opacity 設置透明度Solution. 解決首先我們定義一個 html ...
Question. 問題

在移動端商城系統中,我們常常看到位于頁面頂部有一個搜索框,這類搜索框博主比較喜歡的是固定在頁面頂部,半透明懸浮,能依稀看見部分輪播圖的形式。

html移動端固定懸浮半透明搜索框的實現方法介紹

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

  • fixed 搜索框定位

  • opacity 設置透明度

Solution. 解決

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


<!-- 搜索框 -->
<header class="bar">
  <form name="search" class="search" id="search" action="">
    <p class="search-row">
      <input type="search" name="word" id="word">
      <span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
    </p>
  </form>
</header>
<!-- 一個背景圖 實際上這里往往是輪播圖 -->
<p class="background">
  <img src="bg.jpg">
</p>

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; /* 搜索框半透明效果 */,其他的樣式均為了頁面的排版,排版的細節需要各位讀者自己寫一遍理解,過程可能需要花費點時間。

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

html移動端固定懸浮半透明搜索框的實現方法介紹

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

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

html移動端固定懸浮半透明搜索框的實現方法介紹

用于實現用戶切換輸入時「搜索」位置圖標的切換,原理很簡單,增加和移除 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 class="bar">
  <form name="search" class="search" id="search" action="">
    <p class="search-row">
      <input type="search" name="word" id="word">
      <span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
    </p>
  </form>
</header>
<!-- 一個背景圖 實際上這里往往是輪播圖 -->
<p class="background">
  <img src="bg.jpg">
</p>
</body>
<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>
</html>

以上就是html移動端固定懸浮半透明搜索框的實現方法介紹的詳細內容,更多請關注php中文網其它相關文章!


網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。




主站蜘蛛池模板: 亚洲欧美精品在线 | 日韩不卡手机视频在线观看 | 日本免费v片一二三区 | 三级国产在线观看 | 日韩精品一区二区三区中文3d | 深夜视频免费在线观看 | 欧洲另类一二三四区 | 欧美性猛交xxxx乱大交中文 | 亚洲图片在线欧美专区图片 | 中文字幕欧美视频 | 色综合网址 | 亚洲a视频在线观看 | 越南一级淫片高清视频 | 青青久久精品国产 | 日本久操| 日韩免费专区 | 在线五月婷婷 | 亚洲人成网男女大片在线播放 | 日韩欧美一区二区三区不卡在线 | 日本在线免费 | 日本视频在线免费观看 | 欧美午夜成年片在线观看 | 天堂资源中文在线 | 日本最新免费二区三区 | 日韩黄色大片 | 日韩伦理一区 | 天天躁日日躁狠狠躁黑人躁 | 伊人久久中文 | 日韩一级淫片 | 日本欧美中文字幕 | 青娱极品盛宴国产精品分类 | 天天综合网在线 | 四虎在线精品免费高清在线 | 中文字幕乱码视频中文字幕14 | 青青久草| 日韩三级视频在线观看 | 亚洲大尺度视频 | 日本国产欧美 | 四虎影视永久免费观看网址 | 午夜影院在线视频 | 夭天干天天做天天免费看 |