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

yizhihongxing

实现移动端固定悬浮半透明搜索框通常通过以下步骤:

步骤一:创建HTML结构

在HTML中创建一个搜索框结构,可以使用form和input标签,如下所示:

<form>
  <input type="search" placeholder="搜索关键字">
  <button type="submit">搜索</button>
</form>

步骤二:基本样式设置

设置搜索框样式,包括背景颜色、圆角、边框等,如下所示:

form {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  box-sizing: border-box;
  border-radius: 5px;
  overflow: hidden;
  z-index: 999;
}

input {
  display: block;
  float: left;
  width: 80%;
  height: 30px;
  padding: 0 10px;
  border: none;
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.8);
  box-sizing: border-box;
  font-size: 14px;
  color: #333;
}

button {
  display: block;
  float: right;
  width: 18%;
  height: 30px;
  padding: 0;
  margin: 0;
  border: none;
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.8);
  box-sizing: border-box;
  font-size: 14px;
  color: #333;
  cursor: pointer;
}

步骤三:响应式设置

为了适应移动设备的不同屏幕宽度,可以使用@media查询和rem单位设置响应式样式,如下所示:

@media (max-width: 768px) {
  form {
    height: 40px;
  }

  input {
    width: 70%;
    height: 25px;
    font-size: 12px;
  }

  button {
    width: 26%;
    height: 25px;
    font-size: 12px;
  }
}

@media (max-width: 640px) {
  form {
    height: 30px;
  }

  input {
    width: 60%;
    height: 20px;
    font-size: 10px;
  }

  button {
    width: 30%;
    height: 20px;
    font-size: 10px;
  }
}

示例1:固定悬浮在页面顶部

下面的代码演示了如何将搜索框固定在页面顶部,并且随着页面滚动保持不变:

<body>
  <div class="wrapper">
    <!-- 其他页面内容 -->
    <form>
      <input type="search" placeholder="搜索关键字">
      <button type="submit">搜索</button>
    </form>
  </div>
</body>

<style>
.wrapper { margin-top: 50px; }
form {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  box-sizing: border-box;
  border-radius: 5px;
  overflow: hidden;
  z-index: 999;
}
</style>

<script>
$(function(){
  $(window).scroll(function(){
    if($(window).scrollTop() > 50){
      $('form').addClass('fixed');
    }else{
      $('form').removeClass('fixed');
    }
  });
});
</script>

示例2:仅在页面某一部分出现

下面的代码演示了如何将搜索框放在一个div容器中,仅在页面某一部分出现:

<body>
  <div class="wrapper">
    <div class="header">
      <!-- 页面头部 -->
    </div>
    <div class="main">
      <!-- 页面主要内容 -->
      <div class="search-box">
        <form>
          <input type="search" placeholder="搜索关键字">
          <button type="submit">搜索</button>
        </form>
      </div>
    </div>
    <div class="footer">
      <!-- 页面底部 -->
    </div>
  </div>
</body>

<style>
.search-box {
  position: sticky;
  top: 50px;
  left: 0;
  width: 100%;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 5px;
  overflow: hidden;
  z-index: 999;
}
</style>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:HTML实现移动端固定悬浮半透明搜索框 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • layui的table中显示图片方法

    下面是基于layui的table中显示图片方法的详细攻略。 步骤一:引入相关文件和样式 在使用layui的table展示图片前,首先需要引入layui相关文件和样式。在HTML头部引入以下文件和样式: <head> <!– 引入layui的css文件 –> <link rel="stylesheet" h…

    css 2023年6月10日
    00
  • 个性化blog的方法集锦

    下面我来详细讲解“个性化blog的方法集锦”的完整攻略。 一、选择一个合适的博客平台 首先,选择一个适合自己的博客平台非常重要。目前比较流行的博客平台有博客园、CSDN、简书和Github Pages等。不同的博客平台有不同的特点,需要根据自己的需求进行选择。 二、定制个性化主题 接下来,要了解博客平台提供的主题,并选取一个适合自己的主题。如果博客平台提供的…

    css 2023年6月10日
    00
  • 详解css中background-clip属性的作用

    我为你讲解详解 CSS 中 background-clip 属性的作用。 什么是 background-clip 属性? background-clip 属性用于指定背景颜色或图片的绘制范围(裁剪区域),其值可以为 border-box、padding-box 或 content-box。 border-box:绘制的背景会延伸到边框的外侧。 padding…

    css 2023年6月9日
    00
  • Google Chrome浏览器无法显示hover样式的解决方法

    如何解决Google Chrome浏览器无法显示hover样式? 当我们在使用Google Chrome浏览器访问某些网站时,会发现一些简单的hover样式无法正常显示。这可能是由于Google Chrome浏览器的渲染机制引起的。下面是解决这个问题的几种方法: 方法一:使用JavaScript模拟hover 我们可以使用JavaScript模拟hover来…

    css 2023年6月10日
    00
  • jQuery获取样式中颜色值的方法

    下面是关于“jQuery获取样式中颜色值的方法”的完整攻略。 一、概述 在使用 jQuery 对 DOM 元素进行操作时,有时我们需要获取元素的 CSS 样式中的某个属性的值,如颜色值。jQuery 提供了许多方法来获取样式属性的值,如 .css() 方法、.attr() 方法、.prop() 方法等,但是这些方法通常只能获取 CSS 样式中的属性值,而不能…

    css 2023年6月9日
    00
  • JQuery之拖拽插件实现代码

    JQuery是一种广泛使用的JavaScript库,可以简化对HTML文档的操作和创建动态Web页面等任务。JQuery UI是JQuery的官方用户界面库,为开发人员提供可重用的UI组件和交互性行为。 拖拽技术是Web开发中实现复杂交互的重要组成部分之一。JQuery UI提供了一个易于使用和定制的拖拽插件,并支持许多特性,例如限制在容器内部移动,对齐网格…

    css 2023年6月11日
    00
  • CSS设置边框方法详解

    CSS边框是指在HTML元素周围创建一个线条或区域,以便突出显示该元素。 在CSS中,可用以下属性控制元素的边框样式、颜色和宽度: border-style:指定边框样式(solid、dotted、dashed、double等等) border-color:指定边框颜色 border-width:指定边框宽度 border-top-style、border-…

    Web开发基础 2023年3月20日
    00
  • 如何部署Flask?(详解版)

    部署前准备 1.准备服务器环境(如 Ubuntu、CentOS等)。 2.安装必要的软件和库,包括 Python、pip、virtualenv、Nginx、Gunicorn等: # 安装 Python 和 pip sudo apt-get update sudo apt-get install python3-pip # 升级 pip pip install…

    Flask 2023年3月13日
    00
合作推广
合作推广
分享本页
返回顶部