gpt4 book ai didi

javascript - 在插入另一个元素之前和之后垂直居中搜索框

转载 作者:行者123 更新时间:2023-11-28 04:56:58 25 4
gpt4 key购买 nike

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

最佳答案

我为完成这项工作所做的是使用 jQuery 添加一些代码行。要水平和垂直居中 div 更改 css:

 $('.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 以将其放在页面顶部:

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

在追加结果后为 div 添加 margin-top:

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

这是 CodePen 示例: http://codepen.io/anon/pen/NRZwEJ

关于javascript - 在插入另一个元素之前和之后垂直居中搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com