0

I have an problem with my JavaScript and I'm not sure how to deal with it.

So I'm using this script:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {

        $('nav a').click(function(){
        $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top
        }, 700);
        return false;
        });
    });

The script is making a smooth scroll on my page which I think is really cool, so I don't want to delete it.

But my problem is, that I also have a simple gallery slider on my page, but the JavaScript will effect that gallery slider. It scrolls to the top when clicking on a link in href. The code for the gallery slider is:

            <div id="images">
                <img id="billede1" src="http://i.imgur.com/dL3io.jpg" />
                <img id="billede2" src="http://i.imgur.com/qASVX.jpg" />
                <img id="billede3" src="http://i.imgur.com/fLuHO.jpg" />
                <img id="billede4" src="http://i.imgur.com/5Sd3Q.jpg" />
            </div>
            <div id="slider">
                <a href="#billede1">1</a>
                <a href="#billede2">2</a>
                <a href="#billede3">3</a>
                <a href="#billede4">4</a>
            </div>  

The thing is, I really like the simple code and I'm trying to be better, so I don't want to use some advanced code which I don't understand.

Is is somehow possible to disable the JavaScript not to be working in my div? Or maybe called it something else than the "href"?

I think my "href" is the problem.

So.. Here it is in fiddle: http://jsfiddle.net/62dsqLff/ and you can see the problem.

Really thanks. I appreciate it :-)

7
  • can you post your code in a jsfiddle.net and can you add your code to your post instead of posting images with a screenshot? Commented Dec 15, 2014 at 9:05
  • Yes, sure. Sorry about that :-) Commented Dec 15, 2014 at 9:08
  • give us to ur nav bar code... Commented Dec 15, 2014 at 9:14
  • $('nav a') suggests that the smooth scroll should only work with links inside <nav> element. So if the slider is outside of nav it shouldn't be affected. Commented Dec 15, 2014 at 9:14
  • Hey All :-) I posted it on fiddle now: jsfiddle.net/62dsqLff Pawel - problem is that it also work outside the nav a but it shouldn't :( Commented Dec 15, 2014 at 9:25

2 Answers 2

1

Let me share a fiddle for you JSFIDDLE on this demo I am given new class called .nav for your navbar anchor tags. Then rewrite the script like below.

 $('a.nav').click(function () {
     alert( $($(this).attr('href')).offset().top);
     $('html, body').animate({
         scrollTop: $($(this).attr('href')).offset().top
     }, 700);
     // return false;
 });
Sign up to request clarification or add additional context in comments.

5 Comments

I think I implemented the things you want me to do, but it doesn't seem to work :( jsfiddle.net/62dsqLff/3
It's working.... Please add jquery on js fiddle like this jsfiddle.net/62dsqLff/4
Your fiddle is working,, Bt u forgot to include jquery on that check his demo jsfiddle.net/62dsqLff/5.
I have tried in js fiddle and on my website. I'm quite sure that js fiddle told me I didn't have to include jquery. But anyways I still can't get it to work. Try to scroll manually down to my gallery slider and click on one of the number. You will see my problem afterwards. It scrolls up when I click on one of the numbers. I don't want it to do that.
So I tried to delete the entire script and the gallery slider still "scrolls up". This is weird. Check it out: jsfiddle.net/62dsqLff/6 I don't think it is the script anyway. Can I somehow lock " <a href="#billede1">1</a>" to only scroll up to end of div element. Not sure if I explained it right. lol
0

Use an id for that anchor, instead of a vague "nav a" which will trigger on any a inside your nav element.
Also prevent default action if you are using an a as a button.

The right way to create your "return top" script, is to target a button. Instead of nav a, create a button, give it an id, and target that id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.