可展开的 CSS 搜索字段



我有一个CSS可扩展的搜索字段正在运行,但我想要的是搜索项遍历前两个图标,而不是将它们推到左侧。我虽然可以通过对搜索表单应用较高的 z-index 值和对图标应用较低的值来实现这一点,但唉,我错过了一些东西。

这是一个链接,看看我在尝试什么。TIA寻求任何帮助!

https://codepen.io/anon/pen/Qprjjw#anon-login

这是 HTML

<h3>Search Demo</h3>
<div class="right">
    <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
    <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
    <form id="search">
        <input type="search" placeholder="Search">
    </form>
</div>

这是 CSS

body {
    background: #fff;
    color: #666;
    font: 90%/180% Arial, Helvetica, sans-serif;
    width: 800px;
    max-width: 96%;
    margin: 0 auto;
}
.right { float: right}
a {
    color: #69C;
    text-decoration: none;
}
a:hover {
    color: #F60;
}
h1 {
    font: 1.7em;
    line-height: 110%;
    color: #000;
}
p {
    margin: 0 0 20px;
}
/*
input {
    outline: none;
}
*/
input[type=search] {
  outline: none;
  color: #4a83c0;
    -webkit-appearance: textfield;
    -webkit-box-sizing: content-box;
    font-family: inherit;
    font-size: 100%;
}
input::-webkit-search-decoration,
input::-webkit-search-cancel-button {
    display: none; 
}
input[type=search] {
    background: #ededed url(http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
    border: solid 1px #ccc;
    padding: 9px 10px 9px 32px;
    width: 55px;
    -webkit-border-radius: 10em;
    -moz-border-radius: 10em;
    border-radius: 10em;
    -webkit-transition: all .5s;
    -moz-transition: all .5s;
    transition: all .5s;
}
input[type=search]:focus {
    width: 130px;
    background-color: #fff;
    border-color: #66CC75;
    -webkit-box-shadow: 0 0 5px rgba(109,207,246,.5);
    -moz-box-shadow: 0 0 5px rgba(109,207,246,.5);
    box-shadow: 0 0 5px rgba(109,207,246,.5);
}

input:-moz-placeholder {
    color: #4a83c0;
}
input::-webkit-input-placeholder {
    color: #4a83c0;
}
/* Demo */
#search { display:inline-block;}
#search input[type=search] {
    width: 15px;
    padding-left: 10px;
    color: transparent;
    cursor: pointer;
}
#search input[type=search]:hover {
    background-color: #fff;
}
#search input[type=search]:focus {
    width: 600px;
    padding-left: 32px;
    color: #4a83c0;
    background-color: #fff;
    cursor: auto;
}
#search input:-moz-placeholder {
    color: transparent;
}
#search input::-webkit-input-placeholder {
    color: transparent;
}
您可以将

具有position: absolute;的图像相对于具有类right的div进行定位。

您需要在图像上设置z-index of -1,以使表单覆盖它们。

代码笔

body {
  background: #fff;
  color: #666;
  font: 90%/180% Arial, Helvetica, sans-serif;
  width: 800px;
  max-width: 96%;
  margin: 0 auto;
}
.right {
  float: right
}
a {
  color: #69C;
  text-decoration: none;
}
a:hover {
  color: #F60;
}
h1 {
  font: 1.7em;
  line-height: 110%;
  color: #000;
}
p {
  margin: 0 0 20px;
}
/*
input {
	outline: none;
}
*/
input[type=search] {
  outline: none;
  color: #4a83c0;
  -webkit-appearance: textfield;
  -webkit-box-sizing: content-box;
  font-family: inherit;
  font-size: 100%;
}
input::-webkit-search-decoration,
input::-webkit-search-cancel-button {
  display: none;
}
input[type=search] {
  background: #ededed url(http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
  border: solid 1px #ccc;
  padding: 9px 10px 9px 32px;
  width: 55px;
  -webkit-border-radius: 10em;
  -moz-border-radius: 10em;
  border-radius: 10em;
  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  transition: all .5s;
}
input[type=search]:focus {
  width: 130px;
  background-color: #fff;
  border-color: #66CC75;
  -webkit-box-shadow: 0 0 5px rgba(109, 207, 246, .5);
  -moz-box-shadow: 0 0 5px rgba(109, 207, 246, .5);
  box-shadow: 0 0 5px rgba(109, 207, 246, .5);
}
input:-moz-placeholder {
  color: #4a83c0;
}
input::-webkit-input-placeholder {
  color: #4a83c0;
}
/* Demo 2 */
#search {
  display: inline-block;
}
/* new css */
.right {
  position: relative;
}
.right img {
  position: absolute;
  top: 8px;
  z-index: -1;
}
/* change these values to move the images to where you need */
.right img:nth-of-type(1) {
  right: 40px;
}
.right img:nth-of-type(2) {
  right: 60px;
}
#search input[type=search] {
  width: 15px;
  padding-left: 10px;
  color: transparent;
  cursor: pointer;
  z-index: 5;
}
#search input[type=search]:hover {
  background-color: #fff;
}
#search input[type=search]:focus {
  width: 600px;
  padding-left: 32px;
  color: #4a83c0;
  background-color: #fff;
  cursor: auto;
}
#search input:-moz-placeholder {
  color: transparent;
}
#search input::-webkit-input-placeholder {
  color: transparent;
}
<h3>Demo 2</h3>
<div class="right">
  <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
  <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
  <form id="search">
    <input type="search" placeholder="Search">
  </form>
</div>

将可展开的搜索栏设为position: absolute;,它是父position: relative;(因此可扩展栏保留在其中(。然后,例如,您可以使用填充来定位图像。

body {
  background: #fff;
  color: #666;
  font: 90%/180% Arial, Helvetica, sans-serif;
  width: 800px;
  max-width: 96%;
  margin: 0 auto;
}
/* START OF CHANGE */
.search-container {
  position: relative;
  padding-right: 40px;
}
#search input[type=search] {
  position: absolute;
  top: 0;
  right: 0;
}
/* END OF CHANGE */
.right {
  float: right
}
a {
  color: #69C;
  text-decoration: none;
}
a:hover {
  color: #F60;
}
h1 {
  font: 1.7em;
  line-height: 110%;
  color: #000;
}
p {
  margin: 0 0 20px;
}
/*
input {
	outline: none;
}
*/
input[type=search] {
  outline: none;
  color: #4a83c0;
  -webkit-appearance: textfield;
  -webkit-box-sizing: content-box;
  font-family: inherit;
  font-size: 100%;
}
input::-webkit-search-decoration,
input::-webkit-search-cancel-button {
  display: none;
}
input[type=search] {
  background: #ededed url(http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
  border: solid 1px #ccc;
  padding: 9px 10px 9px 32px;
  width: 55px;
  -webkit-border-radius: 10em;
  -moz-border-radius: 10em;
  border-radius: 10em;
  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  transition: all .5s;
}
input[type=search]:focus {
  width: 130px;
  background-color: #fff;
  border-color: #66CC75;
  -webkit-box-shadow: 0 0 5px rgba(109, 207, 246, .5);
  -moz-box-shadow: 0 0 5px rgba(109, 207, 246, .5);
  box-shadow: 0 0 5px rgba(109, 207, 246, .5);
}
input:-moz-placeholder {
  color: #4a83c0;
}
input::-webkit-input-placeholder {
  color: #4a83c0;
}
/* Demo 2 */
#search {
  display: inline-block;
}
#search input[type=search] {
  width: 15px;
  padding-left: 10px;
  color: transparent;
  cursor: pointer;
}
#search input[type=search]:hover {
  background-color: #fff;
}
#search input[type=search]:focus {
  width: 600px;
  padding-left: 32px;
  color: #4a83c0;
  background-color: #fff;
  cursor: auto;
}
#search input:-moz-placeholder {
  color: transparent;
}
#search input::-webkit-input-placeholder {
  color: transparent;
}
<h3>Demo 2</h3>
<div class="right search-container">
  <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
  <img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png" alt="" width="" height="" border="" align="" />
  <form id="search">
    <input type="search" placeholder="Search">
  </form>
</div>

我用Javascript两个函数解决了你的问题。

对于不覆盖其他图像,请为要隐藏的图像提供 ID。

  • 算法只是改变这两个图像的大小。
  • 需要两个函数,一个是用户点击输入字段时。
    • 在javascript中,它是onClick来调用任何函数。
  • 其次,当用户退出输入字段时。
  • 在javascript中,调用任何函数都是onBlur

继续在你的JavaScript部分添加这两个函数

 // this will set the size of the images to 0px
function setImageWidthZero() {
document.getElementById("img1").style.width='0%';
document.getElementById("img2").style.width='0%';
  }
// This will set the width to their default widths 
function setImageWidthDefault() {
   document.getElementById("img1").style.width='';
   document.getElementById("img2").style.width='';

}

不要忘记给图像 ID .

在输入字段中,您需要同时使用 onblur 和 onClick 方法。

<input type="search" placeholder="Search" onfocus='setImageWidthZero()' 
onblur='setImageWidthDefault()'>

您可以复制的整个代码如下所示:

网页部件

<h3>Demo 2</h3>
<div class="right">
<img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png"
alt="" width="" height="" border="" align="" id='img1'/>
<img src="http://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png"
 alt="" width="" height="" border="" align="" id='img2'/>
<form id="search">
<input type="search" placeholder="Search" onfocus='setImageWidthZero()'
 onblur='setImageWidthDefault()'>
</form>
</div>

CSS部分,不需要在那里添加什么,就可以复制了 从您提供链接的网站。

如有任何问题,请告诉我。享受编码。

相关内容

  • 没有找到相关文章

最新更新