2
votes

Utilisation d'images comme liens d'ancrage cliquables

J'ai un sidenav composé de quelques images. Je veux que ces images soient des liens vers des sections sur la même page - des liens d'ancrage. Cependant, je n'arrive pas à le faire fonctionner.

Voici mon HTML:

#nav2 {
    position: fixed;
    top: 14.285vh;
    cursor: pointer;
}

Voici le style sur # nav2:

<div id="nav2"><a href="#powerlifting"><img src="img/icon1.png" alt=""></a></div>

<a name="powerlifting"></a>
<div id="powerlifting">
    <div class="header1"><h1>POWERLIFTING</h1></div>
</div>

Merci d'avance! :-)


0 commentaires

3 Réponses :


0
votes

Le code ci-dessus semble fonctionner. La balise d'ancrage avec le href de #powerlifting saute en fait à div # powerlifting . Vous ne le voyez peut-être pas, car votre div n'est peut-être pas assez grand.

Si vous voulez qu'il défile vers le bas avec une animation, vous devrez soit utiliser JS ou CSS.

Cela peut être une réponse connexe: Faire défiler jusqu'à une ancre à l'aide de Transition / CSS3 . Le JS Fiddle qui l'accompagne (à partir de la réponse associée) peut vous être utile: http://jsfiddle.net/YYPKM / 3 /


0 commentaires

0
votes

position: fixe ou collant dans la mise en page

Votre la mise en page d'une image et d'un lien est parfaite et dans circonstances normales , vous ne devriez avoir aucun problème. Nous pouvons donc probablement supposer que c'est la disposition environnante conçue autour de la barre de navigation fixe qui paralyse le comportement standard de sauter à un nchor. Au lieu d'attendre plus de votre code (ce qui - sans infraction - est probablement un désordre horrible), j'ai créé une solution HTML / CSS entièrement fonctionnelle pour les problèmes que vous aurez probablement une fois que vous aurez fait fonctionner l'ancre simple.


Démo

Lors de l'examen de ce deno, cliquez sur le lien Pleine page. Cela n'a pas l'air génial dans un cadre aux dimensions non standard, car la majorité des longueurs de la démo sont liées à la fenêtre.

 entrez la description de l'image ici a >

<!DOCTYPE html>
<html>

<head>
  <meta charset='utf-8'>
  <title></title>
  <link href="https://fonts.googleapis.com/css?family=Rye" rel="stylesheet">
  <style>
    /* Basic reset */
    
    * {
      margin: 0;
      padding: 0;
    }
    /* vh and vw units will be used throughout this demo */
    
    html,
    body {
      width: 100vw;
      height: 100vh;
      font: 400 oblique 6vh/1.45 Times;
      overflow: hidden;
    }
    /* Animates scrolling */
    
    body {
      margin: 0 auto;
      overflow-y: scroll;
      scroll-behavior: smooth;
    }
    
    h1 {
      font: 700 small-caps 2rem Rye;
      margin: 0.67em 0 0.67em 5vw;
    }
    
    h2 {
      font: 700 small-caps 1.5rem Rye;
      margin: 0.83em 0;
    }
    
    h3 {
      font: small-caps 1.25rem Rye;
      margin: 1em 0;
    }
    /* Make sure to set z-index to an integer */
    
    nav {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      z-index: 1;
      border-bottom: 2px ridge #000;
      height: 10vh;
      min-height: 50px;
      width: 100%;
      background: #ddd;
    }
    
    nav a {
      text-decoration: none;
      font-style: normal;
      max-height: 10vh;
      min-height: 50px;
      line-height: 16vh;
      vertical-align: top;
    }
    
    a img {
      height: 10vh;
      max-height: 10vh;
      min-height: 50px;
    }
    
    main {
      margin: 0 auto;
    }
    
    section {
      margin: 0 auto;
      padding: 0 5vw;
    }
    
    article {
      margin: 1rem auto 0;
    }
    
    p {
      margin: 1em 0;
    }
    /*
    Compensates for the fixed navbar height offset when jumping 
    to an anchor. https://stackoverflow.com/a/28824157/2813224
    */
    
     :target::before {
      content: "";
      display: block;
      height: 5vh;
      margin-top: -5vh;
    }
    
    .banner-logo {
      text-align: right;
      height: 70vh;
      margin-bottom: 15vh;
    }
    
    .banner-logo img {
      height: 100%;
      width: auto;
    }
  </style>
</head>

<body>
  <nav>
    <a href="#main">
      <img src="http://alexandre.roulois.fr/data/dev/rkl/icon.png" alt=""> Home
    </a>

    <a href="#sec1">
      <img src="https://img2.apk.tools/150/2/0/9/com.tao101.chucknoris.chucknorrisjokes.png" alt=""> Section 1
    </a>

    <a href="#sec2">
      <img src="https://img2.apk.tools/150/2/0/9/com.tao101.chucknoris.chucknorrisjokes.png" alt=""> Section 2
    </a>
  </nav>

  <main>
    <a id='main'>&nbsp;</a>
    <header>
      <h1>All Things Chuck</h1>
      <figure class='banner-logo'>
        <img src='https://webiconspng.com/wp-content/uploads/2017/09/Chuck-Norris-PNG-Image-50835.png'>
      </figure>
    </header>

    <section>
      <a id='sec1'>&nbsp;</a>
      <header>
        <h2>Section 1</h2>
      </header>

      <p>Chuck Norris doesn't churn butter. He roundhouse kicks the cows and the butter comes straight out Chuck Norris doesn't wash his clothes, he disembowels them, Chuck Norris doesn't wear a watch. HE decides what time it is.</p>

      <article>
        <h3>Article 1</h3>
        <p>Chuck Norris will attain statehood in 2009. His state flower will be the Magnolia, Chuck Norris doesn't wear a watch. HE decides what time it is Contrary to popular belief, America is not a democracy, it is a Chucktatorship, Chuck Norris' hand
          is the only hand that can beat a Royal Flush Chuck Norris originally appeared in the "Street Fighter II" video game, but was removed by Beta Testers because every button caused him to do a roundhouse kick. When asked bout this "glitch," Norris
          replied, "That's no glitch." When Chuck Norris roundhouse kicks you and misses it kills 75% of the population that was standing in the direct line of Chuck, But Chuck never misses The leading causes of death in the United States are: 1. Heart
          Disease 2. Chuck Norris 3. Cancer. Outer space exists because it's afraid to be on the same planet with Chuck Norris Chuck Norris is ten feet tall, weighs two-tons, breathes fire, and could eat a hammer and take a shotgun blast standing.</p>
        <p>Chuck Norris is the only man to ever defeat a brick wall in a game of tennis. When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.</p>
      </article>

      <article>
        <h3>Article 2</h3>
        <p>Most people have 23 pairs of chromosomes. Chuck Norris has 72... and they're all poisonous. There is no theory of evolution. Just a list of creatures Chuck Norris has allowed to live. Chuck Norris likes children...they taste like chicken, Chuck
          Norris doesn't churn butter. He roundhouse kicks the cows and the butter comes straight out, After discovering the secrets to time travel, Chuck Norris secretly replaced each player on the 1972 Dolphins team, thus making them undefeated. Chuck
          Norris doesn't dress up for Halloween, he's scary enough Chuck Norris figured out the secret of the universe. He just never told anyone, When Chuck Norris does a pushup, he isn't lifting himself up, he's pushing the Earth down Chuck Norris does
          not get frostbite. Chuck Norris bites frost, Contrary to popular belief, there is one thing faster then the speed of light, Chuck Norris' foot Chuck Norris once roundhouse kicked someone so hard that his foot broke the speed of light, went back
          in time, and killed Amelia Earhart while she was flying over the Pacific Ocean.</p>
      </article>
    </section>

    <section>
      <a id='sec2'>&nbsp;</a>
      <header>
        <h2>Section 2</h2>
      </header>

      <article>
        <p>Outer space exists because it's afraid to be on the same planet with Chuck Norris, There is no theory of evolution. Just a list of animals Chuck Norris allows to live. For every action, there is an unequal and opposite reaction from Chuck Norris
          that kills you, Crop circles are Chuck Norris' way of telling the world that sometimes corn needs to lie down. Chuck Norris is currently suing NBC, claiming Law and Order are trademarked names for his left and right legs Chuck Norris is the
          reason why Waldo is hiding</p>
      </article>

    </section>
  </main>
</body>

</html>

0 commentaires

0
votes

J'ai trouvé la réponse à ma question! L'ajout d'une valeur z-index à # nav1 semble faire l'affaire

#nav2 {
    position: fixed;
    top: 14.285vh;
    cursor: pointer;
    z-index: 9999;
}


0 commentaires