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

标签 javascript jquery html css

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

相关文章:

javascript - 用于选择集合中 'Shift + Clicking' 项的算法

javascript - Angular 和 requirejs,带有注入(inject)器的提供者

javascript - 如何使用 JADE 显示和访问各个键值

javascript window.print() 访问被拒绝

javascript - jQuery 在单击时添加类并在单击另一个时删除

Jquery 如果悬停元素添加按钮 如果不删除按钮

html - 相对于同样响应的容器的绝对位置

javascript - 点击时使用 Facebook Pixel 跟踪注册情况(JS/JQUERY 代码)

javascript - 当我使用 HTML5 "doesn' 时,空格键 `button` t 在 `contenteditable` 元素中工作。我怎样才能让它工作?

javascript - 在不重构代码的情况下正确定位 Modal React