How do you animate an anchor call?

Asked

Viewed 194 times

0

the links are inside a div and the buttons that call them are outside that div, everything is working, I would just like to put an animation in this transition. P.S. the transition is horizontal.

<style type="text/css">

    html, body {margin: 0; padding: 0;}

#telass{
top: 100px;
left: 0;
width:100%;
height: 80%;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;

display: inline-block;
vertical-align: middle;
position: absolute;

}

.tela{
  top: 100px;
  width: 100%;
  height: 200px;
  display: inline-block;
  vertical-align: top;
  height: 1000px;
}
#tela1{
    background-color: red;
}
#tela2{
    background-color: blue;
}
#tela3{
    background-color: yellow;
}

</style>

<script src="js.js"></script>

<a href="#tela1">Tela 1</a>
<a href="#tela2">Tela 2</a>
<a href="#tela3"> Tela 3</a>
<div id="telass">
 <div id = "tela1" class="tela">
 <p>Tela 1</p>

 </div>
 <div id = "tela2" class="tela">
 <p>Tela 2</p>

 </div>
 <div id = "tela3" class="tela">
    <h1>Tela 3</h1>
 </div>

  • I hope it comes out some solution with CSS, gives me the impression that JS for this is an absurd exaggeration. I may be wrong, but looking "over" the code seems to me that CSS solves. The good thing would be for you to edit the question and for an image illustrating the desired effect,

3 answers

4

This answer got so interesting, visually, that I’ll just translate it.

You can do this using CSS only, as suggested by @Bacchus.

For this, you must use the :target, which is intended to connect with some element of your page. This definition is somewhat familiar with the anchor definition as well.

The Example I like is this:

/*
 *Styling
 */

html,body {
        width: 100%;
        height: 100%;
        position: relative; 
}
body {
    overflow: hidden;
}

header {
    background: #fff; 
    position: fixed; 
    left: 0; top: 0; 
    width:100%;
    height: 3.5rem;
    z-index: 10; 
}

nav {
    width: 100%;
    padding-top: 0.5rem;
}

nav ul {
    list-style: none;
    width: inherit; 
    margin: 0; 
}


ul li:nth-child( 3n + 1), #main .panel:nth-child( 3n + 1) {
    background: rgb( 0, 180, 255 );
}

ul li:nth-child( 3n + 2), #main .panel:nth-child( 3n + 2) {
    background: rgb( 255, 65, 180 );
}

ul li:nth-child( 3n + 3), #main .panel:nth-child( 3n + 3) {
    background: rgb( 0, 255, 180 );
}

ul li {
    display: inline-block; 
    margin: 0 8px;
    margin: 0 0.5rem;
    padding: 5px 8px;
    padding: 0.3rem 0.5rem;
    border-radius: 2px; 
    line-height: 1.5;
}

ul li a {
    color: #fff;
    text-decoration: none;
}

.panel {
    width: 100%;
    height: 500px;
    z-index:0; 
    -webkit-transform: translateZ( 0 );
    transform: translateZ( 0 );
    -webkit-transition: -webkit-transform 0.6s ease-in-out;
    transition: transform 0.6s ease-in-out;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;

}

.panel h1 {
    font-family: sans-serif;
    font-size: 64px;
    font-size: 4rem;
    color: #fff;
    position:relative;
    line-height: 200px;
    top: 33%;
    text-align: center;
    margin: 0;
}

/*
 *Scrolling
 */

a[ id= "servicios" ]:target ~ #main article.panel {
    -webkit-transform: translateY( 0px);
    transform: translateY( 0px );
}

a[ id= "galeria" ]:target ~ #main article.panel {
    -webkit-transform: translateY( -500px );
    transform: translateY( -500px );
}
a[ id= "contacto" ]:target ~ #main article.panel {
    -webkit-transform: translateY( -1000px );
    transform: translateY( -1000px );
}
<a id="servicios"></a>
    <a id="galeria"></a>
    <a id="contacto"></a>
    <header class="nav">
        <nav>
            <ul>
                <li><a href="#servicios"> Servicios </a> </li>
                <li><a href="#galeria"> Galeria </a> </li>
                <li><a href="#contacto">Contacta  nos </a> </li>
            </ul>
        </nav>
    </header>

    <section id="main">
        <article class="panel" id="servicios">
            <h1> Nuestros Servicios</h1>
        </article>

        <article class="panel" id="galeria">
            <h1> Mustra de nuestro trabajos</h1>
        </article>

        <article class="panel" id="contacto">
            <h1> Pongamonos en contacto</h1>
        </article>
    </section>

And to change the effect of the animation, you can use the CSS Transform, as shown in the example.

Original Jsfiddle example.

0

Good "an animation" is very comprehensive... But come on, for a side animation you basically need the library jquery 1.5.2, and put the script below before closing the body... this will cause when clicking on the links, occur a smooth scroll between them:

<script>// <![CDATA[
  $(document).ready(function() {
  function filterPath(string) {
  return string
  .replace(/^\//,'')
  .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
  .replace(/\/$/,'');
  }
  $('a[href*=#]').each(function() {
  if ( filterPath(location.pathname) == filterPath(this.pathname)
  && location.hostname == this.hostname
  && this.hash.replace(/#/,'') ) {
  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
  if ($target) {
  var targetOffset = $target.offset().left;
  $(this).click(function() {
  $('#telass').animate({scrollLeft: targetOffset}, 300);
  return false;
  });
  }
  }
  });
  }); // ]]>
  </script>

0

I made an example method to animate the anchor, see: HTML:

<a href="#tela1" class="btn">Tela 1</a>
<a href="#tela2" class="btn">Tela 2</a>
<a href="#tela3" class="btn">Tela 3</a>

Javascript:

$(function(){
    $('.btn').on('click', function (event) {
      var el = $(this).attr('href');
      var tela = [];
      var larg = $(el).width();
       tela['#tela1'] = 0;
       for (var i=2; i <= $('#telass div').length; i++) {
          tela['#tela'+i] = larg;
          larg += larg;
       }
     $('#telass').animate({scrollLeft:tela[el]}, 800);
     });
});

See here the Jsfiddle Running

Browser other questions tagged

You are not signed in. Login or sign up in order to post.