1

I tried almost every solution i found from this website and from other but with no success. What i am doing is rotating a button using CSS3 animation. I am doing this by having the button linked(using "a" element) and then enabling the animation using the pseudo element :target(almost equal to onclick event works in this case).

My problem is that when i click to the button at the first time a hash-tag(#btnq1 in my case) is being added to the URL.

Now, the actual problem, is that i cannot remove this hash-tag when i reload the whole page or if i want to click to that button one more time and re-run the animation. I tried solutions from some Stackoverflow questions like this or this but didn't got any good result.

See below my code:

You can ignore the initial phase of the animation named q1. HTML code:

<header>
<div id="title">
Just a title
</div>
</header>

<main>
Not important text

<div id="q1">
<div class="q1once">
<a href="#btnq1"><button type="button" name="" value="" id="btnq1">Click to rotate</button></a>
</div>
</div>
</main>

CSS code:

#q1{
    height:100%;
    }

.q1once{
    width:20%;
    height:15%;
    position:absolute;
    left:-50%;
    animation-name:q1;
    animation-delay:3s;
    animation-iteration-count:1;
    animation-duration:1.5s;
    animation-fill-mode:forwards;
    }

#btnq1:target{
    animation-name:q1leave;
    animation-iteration-count:1;
    animation-duration:4s;      
    }

@keyframes q1{
50%{
    transform:translate(440%) scaleX(3);
    }
100%{
    transform:translate(450%) scale(1.5,2);
    }
}
@keyframes q1leave{
    0%{transform:rotate(0deg);
        opacity:1;
        }
    100%{
        opacity:0;
        transform:rotate(360deg);
        }

}

You can check my updated jsfiddle as well.

Adding window.location.hash='' on load of the page is a good partial solution because it removes the hashtag when i reload the page. However, it doesn't remove the hashtag after i click at the button at the first time. I want this to happen in order to be able to rotate the button more than once.

Community
  • 1
  • 1
Dchris
  • 2,867
  • 10
  • 42
  • 73

3 Answers3

1

Try this

$('#btnq1').click(function(event){
   event.preventDefault();
   // your action
});

Hashtags will not be added anymore

0

Adding the below solved my issue:

$(window).on('hashchange', function(e){
    history.replaceState ("", document.title, e.originalEvent.oldURL);
});

Found from another question.

Community
  • 1
  • 1
Dchris
  • 2,867
  • 10
  • 42
  • 73
-1

To clear the hash on page load, you can add:

<script>
window.onload = function() { window.location.hash = ''; };
</script>

now, if you also want to clear it when the button is clicked, we can just add an onclick event to the button:

<a href="#btnq1" onclick="window.location.hash = ''">...
kennypu
  • 5,950
  • 2
  • 22
  • 28
  • Down vote is not for me. For when the button is clicked is not working properly. It is remove the hash-tag only every 3 times that i click the button. On that moment it seems that page is forced to reload for some reason – Dchris Apr 22 '14 at 02:10
  • @Dchris try putting the event on the ` – kennypu Apr 22 '14 at 02:11