2017-03-22 14 views
2

拡張可能なCSS検索フィールドがありますが、検索アイテムが前の2つのアイコンに行き渡り、左に押さないようにします。私は検索フォームに高いz-インデックス値を、アイコンに低い値を適用することでこれを達成することができましたが、残念ながら、私は何かを見逃しています。拡張可能CSS検索フィールド

ここに私が何を試みているかを見るためのリンクがあります。 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> 

は、あなたがクラスrightでのdivにposition: absolute;相対で画像を配置することができ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; 
} 

答えて

0

です。

画像上にz-index of -1を設定して、フォームがそれらをカバーするようにする必要があります。

codepen

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 { 
 
\t 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>

0

あなたの拡張可能な検索バー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 { 
 
\t 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>

0

私はJavascriptを二つの機能を使用して問題を解決しました。

他のイメージを上書きしない場合は、隠したいイメージにIDを付けます。

  • アルゴリズムは、2つの画像のサイズを変更するだけです。
  • ユーザーが入力フィールドをクリックするときに2つの機能が必要です。
    • javascriptでは、任意の機能を呼び出すためにonClickです。
  • 第2に、ユーザーが入力フィールドを終了したとき。

が上に行くと、あなたの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イベントで、JavaScriptで

  • 入力フィールドには、onblurメソッドとonClickメソッドの両方を使用する必要があります。

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

    コピーも全体のコードはこのように書き:

    HTMLパート

    <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パート、あなたはそれをから をコピーすることができ、そこに何かを追加する必要はありませんあなたがリンクを与えたウェブサイト

    問題が発生した場合は、お知らせください。 コーディングをお楽しみください。

  • +1

    ありがとうございます。しかし、私のコードで実際に使用している画像は、動的な幅が異なります。実際には、アンカータグの幅に(テキストに基づいて)ピクセル値を持たない背景イメージがあります。それは理にかなっていますか? – soire

    +0

    すべての画像は、HTMLの任意の要素のバックグラウンドで使用されていても、その幅のピクセル値を持っています。 うまく動作しない場合は、使用したアンカータグの幅を変更してみてください。それに応じて、背景の画像も変更される可能性があります。 –

    関連する問題