在过去的几年里,网页设计的一个新趋势是创造全屏网站搜索体验。
这是一个搜索框,它充分利用了网页及其提供的所有房地产,展示了搜索功能和一些非常酷的全屏效果。
这可以使用css3转换和转换属性来完成,并且只需要一点jquery来帮助清理我们的html代码。
1.为全屏网站搜索框创建HTML。请注意,我们在搜索/关闭图标时使用了字体真棒的标志性字体。
- <!-- Close Button -->
- <div class="close-btn">
- <span class="fas fa-times"></span>
- </div>
- <!-- Search Wrapper -->
- <div class="wrapper">
- <!-- Search Button -->
- <div class="search-btn">
- <span class="fas fa-search"></span>
- </div>
- <!-- Search Box -->
- <div class="search-data">
- <input type="text" required>
- <div class="line"></div>
- <label>Type to search..</label>
- <span class="fas fa-search"></span>
- </div>
- </div>
2.必要的CSS和CSS3样式。
- .wrapper, .search-data{
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
- .wrapper{
- position: fixed;
- height: 0px;
- width: 0px;
- border-radius: 100%;
- background: linear-gradient(-135deg, #c850c0, #4158d0);
- transition: all 0.4s linear;
- }
- .wrapper.active{
- height: 4000px;
- width: 4000px;
- }
- .search-btn{
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- height: 60px;
- width: 60px;
- text-align: center;
- cursor: pointer;
- border-radius: 50%;
- background: linear-gradient(-135deg, #c850c0, #4158d0);
- }
- .search-btn span{
- color: #fff;
- font-size: 22px;
- line-height: 60px;
- }
- .search-data{
- position: absolute;
- height: 50px;
- width: 400px;
- display: flex;
- text-align: center;
- /* display: none; */
- }
- .search-data input{
- height: 100%;
- width: 100%;
- background: none;
- border: none;
- outline: none;
- font-size: 22px;
- font-weight: 500;
- color: #fff;
- }
- .search-data .line{
- position: absolute;
- height: 3px;
- width: 100%;
- background: #fff;
- bottom: 0;
- transform: scaleX(0);
- transition: transform 0.4s 0.3s linear;
- }
- .search-data .line.active{
- transform: scaleX(1);
- }
- .search-data label{
- position: absolute;
- top: 50%;
- left: 0;
- font-size: 20px;
- transform: translateY(-50%);
- pointer-events: none;
- color: rgba(255,255,255,0.7);
- }
- .search-data input:valid ~ label{
- opacity: 0;
- }
- .search-data span{
- color: #fff;
- position: absolute;
- width: 50px;
- font-size: 25px;
- right: 0;
- top: 0;
- line-height: 45px;
- cursor: pointer;
- }
- .close-btn{
- position: absolute;
- z-index: 99;
- right: 25px;
- top: 25px;
- font-size: 25px;
- color: #fff;
- cursor: pointer;
- }
- .search-data, .search-data span,
- .search-data label, .close-btn{
- display: none;
- }
3.在结束body标记之前加载最新的jQuery库。
- <script src="/path/to/cdn/jquery.min.js"></script>
4.启用全屏搜索框的主JavaScript(jQuery脚本)。
- $(".search-btn").click(function(){
- $(".wrapper").addClass("active");
- $(this).css("display", "none");
- $(".search-data").fadeIn(500);
- $(".close-btn").fadeIn(500);
- $(".search-data .line").addClass("active");
- setTimeout(function(){
- $("input").focus();
- $(".search-data label").fadeIn(500);
- $(".search-data span").fadeIn(500);
- }, 800);
- });
- $(".close-btn").click(function(){
- $(".wrapper").removeClass("active");
- $(".search-btn").fadeIn(800);
- $(".search-data").fadeOut(500);
- $(".close-btn").fadeOut(500);
- $(".search-data .line").removeClass("active");
- $("input").val("");
- $(".search-data label").fadeOut(500);
- $(".search-data span").fadeOut(500);
- });