ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • navbar 아이콘 누르기에서 이미지를 숨기는 방법
    카테고리 없음 2020. 8. 19. 15:25

    질문

    < i class = "fa fa-bars">아이콘을 눌렀을 때 (작은 화면에서) 이미지를 숨기려고합니다. 다시 누르면 다시 표시됩니다.

    JQuery 또는 Javascript에 익숙하지 않고 CSS에서 visibility : hidden; 을 사용하여 시도했지만 이미지를 숨기지 않습니다.

    참고-이 코드를 새 창에서 실행하고 막대를 보려면 화면을 축소해야합니다.

    이것은 현재 내 navbar입니다.

    그리고 이것은 이미지를 숨기고 싶을 때입니다.

    ja

    .topnav {
      border-width: 1px 0;
      list-style: none;
      margin: 0;
      padding: 0;
      text-align: center;
      overflow: hidden;
      background-color: black;
      padding: 10px;
      -webkit-user-select: none;
    }
    
    .topnav a {
      display: inline-block;
      padding: 10px;
      margin-left: 40px;
      font-family: 'Open Sans';
      color: white;
      text-decoration: none;
      transition: ease-in-out;
      transition-duration: 0.3s;
      transition-delay: 50ms;
      -webkit-user-drag: none;
      -webkit-user-select: none;
    }
    
    
    /* Hide the link that should open and close the topnav on small screens */
    
    .topnav .icon {
      display: none;
    }
    
    .topnav a:hover {
      background-color: rgba(255, 255, 255, 0.884);
      color: black;
      border-radius: 5px;
      transition: ease-in-out;
      transition-duration: 0.4s;
      transition-property: all;
    }
    
    
    /* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
    
    @media screen and (max-width: 600px) {
      .topnav a:not(:first-child) {
        display: none;
      }
      .topnav a.icon {
        float: left;
        display: block;
      }
      .topnav .icon {
        margin-right: 50px;
        margin-top: 5px;
        transition-property: none;
      }
    }
    
    
    /* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
    
    @media screen and (max-width: 600px) {
      .topnav.responsive {
        position: relative;
      }
      .topnav.responsive a.icon {
        position: absolute;
        right: 80%;
        top: 8px;
      }
      .topnav.responsive a {
        float: none;
        display: block;
        text-align: left;
        transition-property: none;
      }
    }
    <html>
    
    <body>
      <link href="https://fonts.googleapis.com/css2?family=Roboto:ital@1&amp;display=swap" rel="stylesheet">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <link href="https://fonts.googleapis.com/css2?family=Open+Sans&amp;display=swap" rel="stylesheet">
      <link href="Stylesheetlol.css" rel="stylesheet">
    
      <script>
        function myFunction() {
          var x = document.getElementById("myTopnav");
          if (x.className === "topnav") {
            x.className += " responsive";
          } else {
            x.className = "topnav";
          }
        }
      </script>
      <!--—Navbar and Logo—-->
      <div class="topnav" id="myTopnav">
        <i class="logo"><img
                    src="https://t4.ftcdn.net/jpg/02/77/45/89/240_F_277458983_6C5chZcmha2MymgJySMsiqIxJvzxgmVd.jpg"
                    style="height:45px;float:right;border-radius: 3px;-webkit-user-drag: none;"></i>
        <a href="#home" class="active">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
        <a href="javascript:void(0);" class="icon" onclick="myFunction()">
          <i class="fa fa-bars"></i>
        </a>
      </div>


    답변1

    로고 이미지 id(myLogo)를 할당하고 display : noneelse 를 사용하면됩니다. responsiveclass가 요소에 적용될 때 script 의 block 입니다.

    라이브 데모 :



     

     

     

     

    출처 : https://stackoverflow.com/questions/63422122/how-to-hide-image-on-navbar-icon-press

    댓글

Designed by Tistory.