简体   繁体   中英

Uncaught ReferenceError: $ is not defined? Jsfiddle

I'm a beginner at javascript and i have used jsfiddle to create a navigation bar which appears when the user has scrolled down.

When i copy the code to dreamweaver it no longer works?

I have researched and it said something about adding the jquery framework or something? Or is there a way to do this without the framework?

Link to jsfiddle for full code: http://jsfiddle.net/fNn7K/270/

javascript :

$(window).on('scroll', function () {
  console.log($(this).scrollTop());
   if ($(this).scrollTop() > 50) {
     $('.nav').addClass('visible');

   }else if ($(this).scrollTop() <= 50 && $('.nav').hasClass('visible')) {
      $('.nav').removeClass('visible');
   }

});

Without jQuery you can do :

window.onscroll = function() {
    var display = document.body.scrollTop > 150 ? 'inline' : 'none',
        elems   = document.getElementsByTagName('nav');

    for (var i=0; i<elems.length; i++) {
        elems[i].style.display = display;
    }
}

FIDDLE

When i copy the code to dreamweaver it no longer works?

JS Fiddle assembles a page based on several pieces of user entered data. One of those pieces of data is the selection of a library.

You have to copy the code to the right places in the document and include the same libraries.

Even then, the preview modes of Dreamweaver might not show it up, because they are (or at least were) entirely awful. Do you testing in a real browser.

I have researched and it said something about adding the jquery framework or something?

You need the jQuery library to use jQuery methods, yes.

Or is there a way to do this without the framework?

jQuery is just some JavaScript written by other people. You can reproduce anything it does. A line by line rewrite of your code to not use jQuery would be out of scope for a stackoverflow answer though.

you need to add jquery.js file in your code (dreamweaver)..

add this in between <head> tag

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

in the fiddle you provided, the jquery is already loaded..so you didn't get that error.

and don't forget to wrap your code inside document.ready function (which is again, already added in fiddle)..

 $(function(){
     $(window).on('scroll', function () {
       .....
     });
 });

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