美觀的搜尋框程式碼例項

antzone發表於2017-04-19

分享一段程式碼例項,它實現了比較美觀的搜尋框效果。

有點類似於百度的搜尋頁面,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  margin: 0;
  padding: 0;
  font-family: "微軟雅黑";
}
input {
  outline: none;
  border: 0;
}
.search_box {
  width: 500px;
  height: 35px;
  border: 2px solid teal;
  margin: 100px auto;
  font-size: 14px;
}
.search_box input, .search_box label {
  height: 35px;
  line-height: 35px;
  padding: 0 10px;
}
.search_box .search_txt {
  float: left;
  width: 420px;
  position: relative;
  overflow: hidden;
}
.search_box .search_txt input {
  width: 100%;
  position: relative;
  background: transparent;
}
.search_box .search_txt label {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  color: #999;
}
.search_box .search_btn {
  float: left;
  width: 80px;
  background: teal;
  font-size: 16px;
  color: #FFF;
  cursor: pointer;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  var temp = 0;
  $(".search_txt input").focus(function () {
    $(this).siblings().hide();
  });
  $(".search_txt input").blur(function () {
    if ($(this).val() == "") {
      $(this).siblings().show();
    }
  });
})
</script>
</head>
<body>
  <div class="search_box">
    <div class="search_txt">
      <label>請輸入需要搜尋的內容</label>
      <input type="text" value="" />
    </div>
    <input type="button" value="搜尋" class="search_btn" />
  </div>
</body>
</html>

相關文章