2016-10-30 9 views
0

前垂直中心検索ボックスと別の要素

$(function() { 
 

 
    $('.forminput input[type="text"]').on('input propertychange', function() { 
 
    var $this = $(this); 
 
    var visible = Boolean($this.val()); 
 
    $this.siblings('.glyphicon').toggleClass('hidden', !visible); 
 
    }).trigger('propertychange'); //nema potrebe za njim 
 

 
    $('.glyphicon').click(function() { 
 
    $(this).siblings('input[type="text"]').val('') 
 
     .trigger('propertychange').focus(); 
 
    $('.results').empty(); 
 
    }); 
 

 
    $('.forminput').on('submit', function(event) { 
 
    event.preventDefault(); 
 
    var typed = $('.nice').val(); 
 
    $.getJSON('http://en.wikipedia.org/w/api.php?callback=?', { 
 
     action: 'query', 
 
     srsearch: typed, 
 
     format: 'json', 
 
     list: 'search' 
 
    }, function(data) { 
 
     $('.results').empty(); 
 
     console.log(data); 
 
     $.each(data.query.search, function(index, item) { 
 
     $('.results').append("<a class='append' href='http://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "'>" + "<div class='appendsearch'><h1>" + item.title + "</h1><p>" + item.snippet + "</p></div></a>") 
 
     }) 
 
    }) 
 
    }) 
 
})
body { 
 
    background: rgb(9, 43, 64); 
 
    font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif; 
 
    height: 90vh; 
 
} 
 

 
.wrapper { 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.container { 
 
    margin-top: 30px; 
 
    display: flex; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.glyphicon { 
 
    color: #B2DFDB; 
 
} 
 

 
.textbox { 
 
    text-align: center; 
 
} 
 

 
.randomArticle { 
 
    color: #B2DFDB; 
 
    font-size: 1.4em; 
 
} 
 

 
.randomArticle:hover { 
 
    text-decoration: none; 
 
    color: pink; 
 
    cursor: pointer; 
 
} 
 

 
.randomArticle:link { 
 
    text-decoration: none; 
 
    color: #B2DFDB; 
 
} 
 

 
form { 
 
    margin-top: 30px; 
 
    margin-bottom: 30px; 
 
} 
 

 
form .nice { 
 
    width: 300px; 
 
    border-radius: 10px; 
 
    border: 5px solid orange; 
 
    background: transparent; 
 
    color: white; 
 
    padding: 7px 15px; 
 
} 
 

 
form .nice:focus { 
 
    outline: none; 
 
} 
 

 
.button { 
 
    border-radius: 10px; 
 
    border: 5px solid orange; 
 
    padding: 7px 15px; 
 
    margin-left: 20px; 
 
    background: transparent; 
 
    color: #B2DFDB; 
 
} 
 

 
.button:hover { 
 
    background: #00897B; 
 
} 
 

 
.button:focus { 
 
    outline: none; 
 
} 
 

 
.append { 
 
    color: black; 
 
} 
 

 
.append:hover { 
 
    text-decoration: none; 
 
} 
 

 
.appendsearch { 
 
    background: rgb(230, 230, 231); 
 
    margin: 20px 70px 20px 70px; 
 
    padding: 10px 20px; 
 
    color: black; 
 
    border-left: 4px solid rgb(9, 43, 64); 
 
    font-weight: 500; 
 
} 
 

 
.appendsearch h1 { 
 
    font-size: 20px; 
 
    font-weight: bold; 
 
} 
 

 
.appendsearch p { 
 
    font-size: 15px; 
 
} 
 

 
.appendsearch:hover { 
 
    border-left: 4px solid orange; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
<div class='container'> 
 
    <div class='wrapper'> 
 
    <div class='textbox'> 
 
     <a class='randomArticle' href='https://en.wikipedia.org/wiki/Special:Random' target='_blank'>Click here for a random article</a> 
 
     <form class='forminput'> 
 
     <input class='nice' type='text'> 
 
     <span class='glyphicon glyphicon-remove hidden'></span> 
 
     <input class='button' type='submit' value='Search'> 
 
     </form> 
 
    </div> 
 
    <div class='results'></div> 
 
    </div> 
 
</div> 
 
</body

を挿入した後、私は、要素が垂直結果が挿入される検索の前後中央に配置するために取得傾けます。私はたくさんのオプションを試しましたが、検索結果の左側に検索ボックスが挿入される状況があります。 サンプルがあります>http://codepen.io/Todorovic/pen/PGrqOp

+1

あなたのコードサンプルは 'キャッチされないにReferenceErrorで実行している場合に発生する最初ので:$ではありませんあなたはそれを最初に修正したいかもしれませんので、 – LGSon

+0

どのように見えるでしょうか?検索結果はどこに挿入されるのですか? – andreas

+0

https://codepen.io/FreeCodeCamp/full/wGqEga/ これは私が手に入れたいものです。それは私のコードのケースではない – pt789

答えて

0

私がこの作業をするために行ったことは、jQueryでいくつかのコード行を追加することでした。

$('.textbox').css({ 
     'position' : 'absolute', 
     'left' : '50%', 
     'top' : '50%', 
     'margin-left' : function() {return -$(this).outerWidth()/2}, 
     'margin-top' : function() {return -$(this).outerHeight()/2} 
    }); 

寸法での作業のためにあなたが慣れていない場合は、チェック:あなたのコード内

http://api.jquery.com/outerwidth/

http://api.jquery.com/outerheight/

そしてダウン後の提出水平方向のdivを中央に、垂直方向のCSSを変更するために フォームの変更をもう一度やってdivのCSSをページの上に置く:

$('.textbox').css({ 
    'position' : 'absolute', 
    'left' : '50%', 
    'top':'0%', 
    'margin-top' : '30px', 
    'margin-left' : function() {return -$(this).outerWidth()/2}, 
}); 

アペンド結果がdiv要素のマージントップを追加した後:ここで

$('.results').css({  
      'margin-top': $('.textbox').height() 
     })  
    }) 

はCodePenの例である: http://codepen.io/anon/pen/NRZwEJ

0

希望する要素は動的ではなく、検索ボックスの周囲に要素の固定構造があります。要素の構造とスタイルを確定したら、textbox要素の高さを特定する必要があります。現時点では47pxです。この値は次のCSSで使用します。

次のスタイルをCSSに追加します。

.textbox { 
    text-align: center; 
    position: absolute; 
    top: 50%; 
    margin-top: -23px; 
} 

textbox要素の半分。)margin-topの値は47pxの半分であることに注意してください

編集

あなたのjqueryのコードに次の行を追加します。

$('.forminput').on('submit', function(event) { 
    $('.textbox').addClass('pull-up'); 

その後、次の追加のスタイルでcssを更新してください。

.textbox { 
    text-align: center; 
    position: absolute; 
    top: 50%; 
    margin-top: -23px; 
} 
.textbox.pull-up { 
    position: relative; 
    top: auto; 
} 
+0

Didntは欲しい結果を得る。検索ボックスは画面の中央に固定されていました。私はそれを上に、検索結果の上に行きたい。 これを実装したい=> https://codepen.io/FreeCodeCamp/full/wGqEga/ – pt789

+0

更新された回答を確認してください。 –

関連する問題