improve responsiveness of transforming header

时间:2018-03-25 20:03:36

标签: javascript html css responsive-design

Here is the code for a header that sort of transforms into a search bar when the search icon is clicked. it looks fine on my laptop but i cant for the life of me get it to work perfectly on my phone. i simply cant get the numbers right.

html :

<div style="position: fixed; width: 100%; box-shadow: 0px 1px grey; transition: .4s ease-in-out;" id="headershell">
        <div id="header">

            <div id="searchicondiv">
                <i class="fas fa-search searchicon" onclick="$hidesearch"></i>
            </div>

            <div id="namediv">
                <h1 id="name">Website</h1>
            </div>

            <div id="logindiv">
                <a href="#"><h2 id="login">Login</h2></a>
            </div>

        </div>

        <div id="hiddensearch" class="Hidden" style="display: flex; background-color: black;">
            <div style="float: left; width: 33.3%;">
                <i class="fas fa-times searchicon" style="color: white;"></i>
            </div>
            <div style="float: left; width: 66.770%;">
                <input id="search" placeholder="Search..">  
            </div>
        </div>

     </div>

css:

div#header {
    width: 100%;
    background-color: rgba(0, 0, 0, 1);
    display: flex;
    z-index: 1000000;
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
}

div#header #name {
    color: white;
    font-family: orbitron;
    text-align: center;
    font-size: 1.5em;
    margin-left: 5%;
}

div#header #login {
    color: white;
    font-family: -apple-system, BlinkMacSystemFont, sans-serif;
    text-align: center;
    font-size: 1em;
    transition: .2s ease-in-out;
    padding: 0px;
}

div#headershell .searchicon {
    color: white;
    margin: auto;
    margin-left: 50%;
    transition: .2s ease-in-out;
}

div#headershell .searchicon:hover, #login:hover {
    cursor: pointer;
    opacity: 0.5;
}

div#namediv {
    float: left;
    width: 33.4%;
}

div#searchicondiv {
    width: 33.3%;
    margin: auto;
    transition: .2s ease-in-out;
}

div#logindiv {
    width: 33.3%;
    float: left;
    margin: auto;
}

div#hiddensearch {
    z-index: 100;
    transition: 0.4s ease-in-out;
    padding-bottom: 1em;
    height: inherit;
    background-color: rgba(0, 0, 0, 1);
}

div#hiddensearch  #search {
    padding: .4em;
    width: 80%;
    margin-left: 10%;
    margin-right: 10%;
    border-radius: 5px;
    margin-top: -5%;
    color: white;
    background-color: rgba(0, 0, 0, 1);
    border: 2px white solid;
    transition: 0.2s ease-in-out;
}

div#hiddensearch  #search:focus {
    text-decoration: none;
    color: black;
    background-color: white;
    border: 2px black solid;
    outline-width: 0px;
}

.headernotHidden {
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
}


.notHidden {
    margin-top: 0%;
    padding-top: 22%; 
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    opacity: 1;
}

.Hidden {
    margin-top: -15%;
}

.headershellHidden{
    margin-top: -25%;
}

@media only screen and (max-width: 950px) {
    .notHidden {
        margin-top: 0%;
        padding-top: 21%; 
}

@media only screen and (max-width: 800px) {

    div#header #name {
        font-size: 1.2em;
    } 

    div#header #namediv {
        width: 50%;
    }

    div#header #searchicondiv {
        width: 25%;
    }

    div#header #logindiv {
        width: 25%;
    }

    img {
        max-width: 60%;
    }

    .Hidden {
        margin-top: -25%;
    }

    .notHidden {
        padding-top: 15%;
    }

}


@media only screen and (max-width: 600px) {

    div#header #name {
    }

    div#header #namediv {
        width: 60%;
    }

    div#header #searchicondiv {
        width: 20%;
    }

    div#header #logindiv {
        width: 20%;
    }

}

js :

$(document).ready(function(){

    // jQuery methods go here...

    $(".searchicon").click (function hidesearch() {

        if ($("#hiddensearch").hasClass("Hidden")){
            $( "#hiddensearch" ).removeClass( "Hidden" );
            $( "#hiddensearch" ).addClass( "notHidden" );

            //$( ".searchicon" ).removeClass( "fa-search" );
            //$( ".searchicon" ).addClass( "fa-times" );

            $( "#headershell" ).addClass( "headershellHidden" );

        }
        else {
            $( "#hiddensearch" ).removeClass( "notHidden" );
            $( "#hiddensearch" ).addClass( "Hidden" );

            //$( ".searchicon" ).removeClass( "fa-times" );
            //$( ".searchicon" ).addClass( "fa-search" );

            $( "#headershell" ).removeClass( "headershellHidden" );
        }
    })

 });

all i want is a little help with making this more responsive and i would love some tips on improving my methods.
(ps: here's the link to the font awesome CSS file for the icons: )

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/fontawesome.css" integrity="sha384-q3jl8XQu1OpdLgGFvNRnPdj5VIlCvgsDQTQB6owSOHWlAurxul7f+JpUOVdAiJ5P" crossorigin="anonymous">

1 个答案:

答案 0 :(得分:0)

Sadly, I cannot change the icon size, but here is the HTML Code I made, This works for me:

$(document).ready(function(){

    // jQuery methods go here...

    $(".searchicon").click (function hidesearch() {

        if ($("#hiddensearch").hasClass("Hidden")){
            $( "#hiddensearch" ).removeClass( "Hidden" );
            $( "#hiddensearch" ).addClass( "notHidden" );

            //$( ".searchicon" ).removeClass( "fa-search" );
            //$( ".searchicon" ).addClass( "fa-times" );

            $( "#headershell" ).addClass( "headershellHidden" );

        }
        else {
            $( "#hiddensearch" ).removeClass( "notHidden" );
            $( "#hiddensearch" ).addClass( "Hidden" );

            //$( ".searchicon" ).removeClass( "fa-times" );
            //$( ".searchicon" ).addClass( "fa-search" );

            $( "#headershell" ).removeClass( "headershellHidden" );
        }
    })

 });
div#header {
    width: 100%;
    background-color: rgba(0, 0, 0, 1);
    display: flex;
    z-index: 1000000;
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
}

div#header #name {
    color: white;
    font-family: orbitron;
    text-align: center;
    font-size: 1.5em;
    margin-left: 5%;
}

div#header #login {
    color: white;
    font-family: -apple-system, BlinkMacSystemFont, sans-serif;
    text-align: center;
    font-size: 1em;
    transition: .2s ease-in-out;
    padding: 0px;
}

div#headershell .searchicon {
    color: white;
    margin: auto;
    margin-left: 50%;
    transition: .2s ease-in-out;
}

div#headershell .searchicon:hover, #login:hover {
    cursor: pointer;
    opacity: 0.5;
}

div#namediv {
    float: left;
    width: 33.4%;
}

div#searchicondiv {
    width: 33.3%;
    margin: auto;
    transition: .2s ease-in-out;
}

div#logindiv {
    width: 33.3%;
    float: left;
    margin: auto;
}

div#hiddensearch {
    z-index: 100;
    transition: 0.4s ease-in-out;
    padding-bottom: 1em;
    height: inherit;
    background-color: rgba(0, 0, 0, 1);
}

div#hiddensearch  #search {
    padding: .4em;
    width: 80%;
    margin-left: 10%;
    margin-right: 10%;
    border-radius: 5px;
    margin-top: -5%;
    color: white;
    background-color: rgba(0, 0, 0, 1);
    border: 2px white solid;
    transition: 0.2s ease-in-out;
}

div#hiddensearch  #search:focus {
    text-decoration: none;
    color: black;
    background-color: white;
    border: 2px black solid;
    outline-width: 0px;
}

.headernotHidden {
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
}


.notHidden {
    margin-top: 0%;
    padding-top: 22%; 
    -webkit-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    box-shadow: 0px 3px 10px 1px rgba(0,0,0,0.75);
    opacity: 1;
}

.Hidden {
    margin-top: -15%;
}

.headershellHidden{
    margin-top: -25%;
}

@media only screen and (max-width: 950px) {
    .notHidden {
        margin-top: 0%;
        padding-top: 21%; 
}

@media only screen and (max-width: 800px) {

    div#header #name {
        font-size: 1.2em;
    } 

    div#header #namediv {
        width: 50%;
    }

    div#header #searchicondiv {
        width: 25%;
    }

    div#header #logindiv {
        width: 25%;
    }

    img {
        max-width: 60%;
    }

    .Hidden {
        margin-top: -25%;
    }

    .notHidden {
        padding-top: 15%;
    }

}


@media only screen and (max-width: 600px) {

    div#header #name {
    }

    div#header #namediv {
        width: 60%;
    }

    div#header #searchicondiv {
        width: 20%;
    }

    div#header #logindiv {
        width: 20%;
    }

}
<script defer src="https://use.fontawesome.com/releases/v5.0.8/js/solid.js" integrity="sha384-+Ga2s7YBbhOD6nie0DzrZpJes+b2K1xkpKxTFFcx59QmVPaSA8c7pycsNaFwUK6l" crossorigin="anonymous"></script>
<script defer src="https://use.fontawesome.com/releases/v5.0.8/js/fontawesome.js" integrity="sha384-7ox8Q2yzO/uWircfojVuCQOZl+ZZBg2D2J5nkpLqzH1HY0C1dHlTKIbpRz/LG23c" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<div class="w3-bar w3-black">
  <a href="#" class="w3-bar-item w3-button w3-mobile"><div id="searchicondiv">
                <i class="fas fa-search searchicon" onclick="$hidesearch"></i>
            </div></a>
  <a href="#" class="w3-bar-item w3-button w3-mobile"><h1 id="name">Website</h1></a>
  <a href="#" class="w3-bar-item w3-button w3-mobile"><h2 id="login">Login</h2></a>
</div>

<div style="position: fixed; width: 100%; box-shadow: 0px 1px grey; transition: .4s ease-in-out;" id="headershell">


        <div id="hiddensearch" class="Hidden" style="display: flex; background-color: black;">
            <div style="float: left; width: 33.3%;">
                <i class="fas fa-times searchicon" style="color: white;"></i>
            </div>
            <div style="float: left; width: 66.770%;">
                <input id="search" placeholder="Search..">  
            </div>
        </div>

     </div>

Note: I have also added a CSS called WC.CSS. You can use that to help improve your website. Edit: I have noticed some Sizing issues, I do not know how to fix this either.

相关问题