简体   繁体   中英

Prevent anchor tags jump to top of page while using target php

for a school project i'm working on a site. Now I am working on a view buttons that show different divs, I came quite far but have this annoying thing going on. If i click an anchor tag it will show the div but it will set me at the top of the page.

I tried doing it with javascript using return false or e.preventdefault but then my divs won't show.

I hope that someone can help me out.

<div class="nav-referenties">
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?target=Alles">ALLES</a>
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?target=Websites">WEBSITES</a>
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?target=Huisstijlen">HUISSTIJLEN</a>
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?target=Verpakkingen">VERPAKKINGEN</a>
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?target=Diversen">DIVERSEN</a>
        <hr class="onderlijn-referenties">
      </div>
      </div>
</div>

<?php


switch(@$_GET['target']) {
  case 'Alles': default :
    echo '<div class="row">
      <div class="col-lg-3 col-sm-6 col-xs-12">
        <div class="arrow-left">
          <i class="fa fa-chevron-circle-left" aria-hidden="true"></i>
        </div>
        <div class="items-referenties">
          <div class="img-veld-referenties-1">
            <img src="Images/items-referenties/1.png" alt="">
          </div>
          <h1>lorem ipsum</h1>
          <hr>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
        </div>
      </div>

      <div class="col-lg-3 col-sm-6 col-xs-12">
        <div class="items-referenties">
          <div class="img-veld-referenties-2">
            <img src="Images/items-referenties/2.png" alt="">
          </div>
          <h1>lorem ipsum</h1>
          <hr>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
        </div>
      </div>

      <div class="col-lg-3 col-sm-6 col-xs-12">
        <div class="items-referenties">
          <div class="img-veld-referenties-1">
            <img src="Images/items-referenties/3.png" alt="">
          </div>
          <h1>lorem ipsum</h1>
          <hr>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
        </div>
      </div>

      <div class="col-lg-3 col-sm-6 col-xs-12">
        <div class="arrow-right">
          <i class="    fa fa-chevron-circle-right" aria-hidden="true"></i>
        </div>
        <div class="items-referenties">
          <div class="img-veld-referenties-2">
            <img src="Images/items-referenties/4.png" alt="">
          </div>
          <h1>lorem ipsum</h1>
          <hr>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
        </div>
      </div>
    </div>';

  break;

  case 'Websites':
    echo "Hier zijn nog geen items voor.<br /><br /><br /><br />";
  break;

  case 'Huisstijlen':
    echo "Hier zijn nog geen items voor.<br /><br /><br /><br />";
  break;

  case 'Verpakkingen':
    echo "Hier zijn nog geen items voor.<br /><br /><br /><br />";
  break;

  case 'Diversen':
    echo "Hier zijn nog geen items voor.<br /><br /><br /><br />";
  break;
}
?>

`

You do not need to use PHP. Simply you can do this only with HTML by the following

// Links
<div class="nav-referenties">
    <a href="#Alles">ALLES</a>
    <a href="#Websites">WEBSITES</a>
    <a href="#Huisstijlen">HUISSTIJLEN</a>
    <a href="#Verpakkingen">VERPAKKINGEN</a>
    <a href="#Diversen">DIVERSEN</a>
    <hr class="onderlijn-referenties">
</div>

// Divs
<div id="Alles"></div>
<div id="Websites"></div>
<div id="Huisstijlen"></div>
<div id="Verpakkingen"></div>
<div id="Diversen"></div>

Again if you want some dynamic actions and animations, you can use Animate Scroll plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM