a澳门星际娱乐:HTML实现移动固定浮动半透明搜索框
在移动端商城系统中,我们常常看到位于页面顶部有一个搜索框,这类搜索框博主比较喜欢的是固定在页面顶部,半透明悬浮,能依稀看见部分轮播图的形式。
要制作这样的搜索框,技术关键在于:
- fixed 搜索框定位
- opacity 设置透明度
Solution. 解决
首先我们定义一个 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>
<!-- 一个背景图 实际上这里往往是轮播图 -->
<div class="background">
<img src="bg.jpg">
</div>
header 标签为搜索框,下面的 div 为一个背景图。
同时附上 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; /* 搜索框半透明效果 */,其他的样式均为了页面的排版,排版的细节需要各位读者自己写一遍理解,过程可能需要花费点时间。
这样我们就完成了一个静态的搜索框:

备注:这里的搜索图标使用了 iconfont,读者可自行到 iconfont矢量图标库 下载。
至此,我们还需要通过 JS 实现一些动效:
用于实现用户切换输入时「搜索」位置图标的切换,原理很简单,增加和移除 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="">
<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>
<!-- 一个背景图 实际上这里往往是轮播图 -->
<div class="background">
<img src="bg.jpg">
</div>
</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>
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/rumen/yyll/y50961.shtml
我要分享到:
必备CSS教程 Essential CSS Tutorials
- • css height
- • css line-height
- • css width
- • css min-width
- • css max-width
- • css min-height
- • css max-height
- • css border
- • css background
- • css float
- • css clear
- • css display
- • css font
- • css text-transform
- • css英文首字母大写
- • css font-variant
- • css font-weight
- • css font-style
- • css text-decoration
- • css 删除线
- • div css 虚线
- • css 注释
- • html 注释
- • css padding
- • css margin
- • css 文本
- • css font-size
- • css font-family
- • css color
- • css text-align
- • css text-indent
- • css 超链接(css a)
- • css 优化压缩
- • css id(css #)
- • css class(css .)
- • css ul li列表
- • css 圆角圆边
- • css 父级子级
- • css 指针概念
- • css cursor
- • css overflow
- • html px em pt网页单位
- • CSS important
- • CSS position
- • css z-index
- • css white-space
- • css img图片
- • css class id
- • css link与@import区别
- • css 选择器
- • css引入html
必备HTML基础教程 Essential HTML Tutorials
- • html img图片标签
- • html em标签(EM强调标签)
- • html strong加粗(strong标签)
- • html B加粗(b加粗标签)
- • strong与B加粗区别
- • h1 h2 h3 h4标签(html标题标签)
- • html A超链接锚文本
- • html注释
- • html head头部标签
- • html title标题标签
- • html meta标签
- • html link标签
- • html i斜体标签
- • html u下划线标签
- • html s删除线标签
- • html换行br标签
- • html p段落标签
- • p标签与br标签区别
- • html div标签元素
- • html span标签
- • html font标签
- • html script标签
- • html px em pt网页单位
- • html ul li列表
- • ol li列表
- • dl dt dd标签组
- • table tr td表格
- • table tr th表格
- • html form表单
- • html form input
- • html form textarea文本区域
- • html select下拉与跳转(Html select)
- • html iframe框架
- • html网页结构
- • htm html shtml区别用法
- • 网页编码charset
- • UTF-8 GBK UTF8 GB2312区别联系
- • 先写html还是先写CSS
- • 显示扩展名
- • html标签大全集合
- • html常用标签
- • 网页源代码是什么
如对文章有任何疑问请提交到DIV CSS论坛,或有任何网页制作CSS问题立即到CSS论坛发贴求解 或 直接DIVCSS5网页顶部搜索遇到DIVCSS疑问。
CSS教程文章修订日期:2018-10-22 11:23 原创:DIVCSS5
本文www.divcss5.com DIVCSS5版权所有。
最新文章NEWS
- • 虚拟主机相关知识
- • DIV CSS加载失败
- • DIV+CSS规范命名大全集合
- • CSS margin属性与用法教程
- • padding_css padding用法详解
- • DIV+CSS中让布局居中_背景图片居中_文字内容居中
- • html与xhtml的区别规范是什么
- • DIV+CSS与TABLE的网页优势何在?
- • 浅谈DIV+CSS设计开发的Xhtml网页对SEO优化的影响
- • 开发DIV+CSS的工具集合
CSS 特效CSS EFFECTS
纯DIV+CSS下拉菜单模块模板
DIV+CSS分页_CSS翻页代码模板
css form实例 用CSS实现表单form布局实例
经典DIV+CSS下拉菜单
div+css不间断上下滚动模板
向上不间断滚动div+css+js模板
相关文章RELATED
- • 澳门棋牌官网:一个因@click.stop引发的bug的解决
- • 澳门银河赌城网站:倔强的程序员
- • 金沙平台:spring boot添加新模块的方法教程
- • 辉煌国际网站:关于php unset对json_encode的影响详解
- • 3u娱乐场:Vue路由器基本使用和配置教程
- • 北京黑彩网:与我一步一步学习判断和选择的语言
- • a澳门星际娱乐:HTML实现移动固定浮动半透明搜索框
- • 大家可以在娱乐城学习赌博游戏的具体打法
- • 娱乐城中有很多适合参与赌博游戏的玩家
- • 在娱乐城提高自己的游戏境界


