Home Web Front-end HTML Tutorial HTML implements fixed floating translucent search box on mobile terminal

HTML implements fixed floating translucent search box on mobile terminal

May 18, 2018 am 10:07 AM
html fixed levitate

Now there are thousands of websites on the Internet, but an indispensable function of the website is search. We can see that the search boxes of many websites are different, and the same is true on the mobile terminal. In this article, we will share with you a search box that is fixed at the top of the page on the mobile side, semi-transparent and suspended, and part of the carousel can be vaguely seen.

HTML implements fixed floating translucent search box on mobile terminal

To create such a search box, the key technology lies in:

fixed search box positioning

opacity setting transparency

Solution. Solution

First we define an html fragment:

1

2

3

4

5

6

7

8

9

10

11

12

13

<!-- 搜索框 -->

<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="/static/imghw/default1.png"  data-src="bg.jpg"  class="lazy"   alt="HTML implements fixed floating translucent search box on mobile terminal" >

</p>

Copy after login

The header tag is the search box, and the p below is a background image.

Attached is the CSS style:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

<style type="text/css">

body {

  margin: 0;  padding: 0;

  font-size: 14px; font-family: "microsoft yahei",&#39;Arial&#39;, &#39;Verdana&#39;,&#39;Helvetica&#39;, 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>

Copy after login

It’s a long CSS style, but its core is just two sentences: position: fixed; /* determines the top of the search box*/ and background-color : #fff; opacity: 0.8; /* Search box translucency effect */, other styles are for the layout of the page. The details of the layout need to be written and understood by readers, and the process may take some time.

In this way we have completed a static search box:

HTML implements fixed floating translucent search box on mobile terminal

Note: The search icon here uses iconfont, readers can go to the iconfont vector icon library by themselves download.

At this point, we still need to implement some animation effects through JS:

HTML implements fixed floating translucent search box on mobile terminal

is used to switch the "search" position icon when the user switches input. The principle is very simple. Simple, add and remove classes, these classes define styles.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

.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>

Copy after login

Note: jQuery needs to be introduced here, don’t forget it!

Extension. Extension

Complete html code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

<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",&#39;Arial&#39;, &#39;Verdana&#39;,&#39;Helvetica&#39;, 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>

 

 

<!-- 搜索框 -->

<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="/static/imghw/default1.png"  data-src="bg.jpg"  class="lazy"   alt="HTML implements fixed floating translucent search box on mobile terminal" >

</p>

 

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

Copy after login

The above content is a tutorial on how to implement a fixed floating translucent search box on the mobile terminal in HTML. I hope you can help during development. Everyone.

Related recommendations:

Css to create a good-looking search box

How to use Js to implement the Baidu search box prompt function

Share 8 CSS3 search boxes

The above is the detailed content of HTML implements fixed floating translucent search box on mobile terminal. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles