当前位置:Gxlcms > html代码 > HTML实现移动端固定悬浮半透明搜索框

HTML实现移动端固定悬浮半透明搜索框

时间:2021-07-01 10:21:17 帮助过:267人阅读

 Question. 问题

在移动端商城系统中,我们常常看到位于页面顶部有一个搜索框,这类搜索框博主比较喜欢的是固定在页面顶部,半透明悬浮,能依稀看见部分轮播图的形式。

要制作这样的搜索框,技术关键在于:

  • fixed 搜索框定位
  • opacity 设置透明度

Solution. 解决

首先我们定义一个 html 片段:

  1. <!-- 搜索框 -->
  2. <header class="bar">
  3. <form name="search" class="search" id="search" action="">
  4. <div class="search-row">
  5. <input type="search" name="word" id="word">
  6. <span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
  7. </div>
  8. </form>
  9. </header>
  10. <!-- 一个背景图 实际上这里往往是轮播图 -->
  11. <div class="background">
  12. <img src="bg.jpg">
  13. </div>

header 标签为搜索框,下面的 div 为一个背景图。

同时附上 CSS 样式:

  1. <style type="text/css">
  2. body {
  3. margin: 0; padding: 0;
  4. font-size: 14px; font-family: "microsoft yahei",'Arial', 'Verdana','Helvetica', sans-serif;
  5. }
  6. .bar {
  7. position: fixed; top: 0; left: 0; right: 0; /* 决定了搜索框置顶 */
  8. height: 44px; padding: 0 10px;
  9. background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */
  10. z-index: 10;
  11. }
  12. .bar form {
  13. display: block; padding: 0;margin: 0;
  14. }
  15. .search-row {
  16. position: relative;
  17. height: 30px; padding: 7px 0;
  18. }
  19. .search-row input[type=search] {
  20. position: absolute; top: 7px;
  21. height: 30px; line-height: 21px; width: 100%; padding: 10px 15px 10px 30px;
  22. border: 0; border-radius: 6px; outline: 0; background-color: rgba(0,0,0,0.1);
  23. font-size: 16px; text-align: center;
  24. z-index: 100;
  25. }
  26. .search-row .placeholder {
  27. position: absolute; top: 2px; left: 0; right: 0;
  28. display: inline-block; height: 34px; line-height: 34px;
  29. border: 0; border-radius: 6px;
  30. font-size: 16px; text-align: center; color: #999;
  31. z-index: 1;
  32. }
  33. .search-row .placeholder .iconfont {
  34. display: inline-block; width: 19px; line-height: 24px; padding: 10px 0;
  35. font-size: 21px; color: #666;
  36. }
  37. .search-row .placeholder .text {
  38. line-height: 40px;
  39. vertical-align: top;
  40. }
  41. .background img {
  42. width: 100%;
  43. }
  44. .active:before {
  45. position: absolute; top: 11px; left: 5px; right: auto;
  46. display: block; margin-right: 0;
  47. font-size: 21px;
  48. }
  49. .active input[type=search] {
  50. text-align: left
  51. }
  52. .active .placeholder{
  53. display: none
  54. }
  55. </style>

很长的一段 CSS 样式,但是其核心就两句话position: fixed; /* 决定了搜索框置顶 */ 和 background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */,其他的样式均为了页面的排版,排版的细节需要各位读者自己写一遍理解,过程可能需要花费点时间。

这样我们就完成了一个静态的搜索框:

备注:这里的搜索图标使用了 iconfont,读者可自行到 iconfont矢量图标库 下载。

至此,我们还需要通过 JS 实现一些动效:

用于实现用户切换输入时「搜索」位置图标的切换,原理很简单,增加和移除 class 类,这些类定义了样式。

  1. .active:before {
  2. position: absolute; top: 11px; left: 5px; right: auto;
  3. display: block; margin-right: 0;
  4. font-size: 21px;
  5. }
  6. .active input[type=search] {
  7. text-align: left
  8. }
  9. .active .placeholder{
  10. display: none
  11. }
  12. <script type="text/javascript">
  13. /* 输入框获取到焦点 表示用户正在输入 */
  14. $("#word").focusin(function() {
  15. $(".search-row").addClass("active iconfont icon-sousuo");
  16. });
  17. /* 输入框失去焦点 表示用户输入完毕 */
  18. $("#word").focusout(function() {
  19. /* 判断用户是否有内容输入 */
  20. if ($(this).val()=="") {
  21. /* 没有内容输入 改变样式 */
  22. $(".search-row").removeClass("active iconfont icon-sousuo");
  23. } else {
  24. /* 有内容输入 保持样式 并提交表单 */
  25. $("#search").submit();
  26. }
  27. });
  28. </script>

备注:这里需要引入 jQuery,千万别忘了!

Extension. 扩展

完整 html 代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
  6. <link rel="stylesheet" type="text/css" href="iconfont/iconfont.css">
  7. <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
  8. <style type="text/css">
  9. body {
  10. margin: 0; padding: 0;
  11. font-size: 14px; font-family: "microsoft yahei",'Arial', 'Verdana','Helvetica', sans-serif;
  12. }
  13. .bar {
  14. position: fixed; top: 0; left: 0; right: 0; /* 决定了搜索框置顶 */
  15. height: 44px; padding: 0 10px;
  16. background-color: #fff; opacity: 0.8; /* 搜索框半透明效果 */
  17. z-index: 10;
  18. }
  19. .bar form {
  20. display: block; padding: 0;margin: 0;
  21. }
  22. .search-row {
  23. position: relative;
  24. height: 30px; padding: 7px 0;
  25. }
  26. .search-row input[type=search] {
  27. position: absolute; top: 7px;
  28. height: 30px; line-height: 21px; width: 100%; padding: 10px 15px 10px 30px;
  29. border: 0; border-radius: 6px; outline: 0; background-color: rgba(0,0,0,0.1);
  30. font-size: 16px; text-align: center;
  31. z-index: 100;
  32. }
  33. .search-row .placeholder {
  34. position: absolute; top: 2px; left: 0; right: 0;
  35. display: inline-block; height: 34px; line-height: 34px;
  36. border: 0; border-radius: 6px;
  37. font-size: 16px; text-align: center; color: #999;
  38. z-index: 1;
  39. }
  40. .search-row .placeholder .iconfont {
  41. display: inline-block; width: 19px; line-height: 24px; padding: 10px 0;
  42. font-size: 21px; color: #666;
  43. }
  44. .search-row .placeholder .text {
  45. line-height: 40px;
  46. vertical-align: top;
  47. }
  48. .background img {
  49. width: 100%;
  50. }
  51. .active:before {
  52. position: absolute; top: 11px; left: 5px; right: auto;
  53. display: block; margin-right: 0;
  54. font-size: 21px;
  55. }
  56. .active input[type=search] {
  57. text-align: left
  58. }
  59. .active .placeholder{
  60. display: none
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <!-- 搜索框 -->
  66. <header class="bar">
  67. <form name="search" class="search" id="search" action="">
  68. <div class="search-row">
  69. <input type="search" name="word" id="word">
  70. <span class="placeholder "><span class="iconfont icon-sousuo"></span><span class="text">搜索</span></span>
  71. </div>
  72. </form>
  73. </header>
  74. <!-- 一个背景图 实际上这里往往是轮播图 -->
  75. <div class="background">
  76. <img src="bg.jpg">
  77. </div>
  78. </body>
  79. <script type="text/javascript">
  80. /* 输入框获取到焦点 表示用户正在输入 */
  81. $("#word").focusin(function() {
  82. $(".search-row").addClass("active iconfont icon-sousuo");
  83. });
  84. /* 输入框失去焦点 表示用户输入完毕 */
  85. $("#word").focusout(function() {
  86. /* 判断用户是否有内容输入 */
  87. if ($(this).val()=="") {
  88. /* 没有内容输入 改变样式 */
  89. $(".search-row").removeClass("active iconfont icon-sousuo");
  90. } else {
  91. /* 有内容输入 保持样式 并提交表单 */
  92. $("#search").submit();
  93. }
  94. });
  95. </script>
  96. </html>

总结

以上所述是小编给大家介绍的HTML实现移动端固定悬浮半透明搜索框,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行