a couple of days ago i started working with my companies website. I saw it was missing something so I decided to make a scroll to top button with animation. I am a noob in programming so i decided to look on the Internet. There I found this example. I literally copied the whole code but it does not work.
the button in the html:
<i class="icon-chevron-up"></i>
and this is the jQuery:
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) {
$('#return-to-top').fadeIn(200);
} else {
$('#return-to-top').fadeOut(200);
}
});
$('#return-to-top').click(function() {
$('body, html').animate({
scrollTop : 0
}, 500);
});
I dont know what the problem is because it works on the codepen. Does anyone know how to fix this?
Thanks in advance
There is not enough information but if you use the same code from codepen then maybe you need to include jQuery into your site header. (https://cdnjs.com/libraries/jquery/)
You should do something like this :
$('#return-to-top').click(function(e) {
e.preventDefault();
$('body, html').stop().animate({
scrollTop : 0
}, 500);
});
And first of all, check if you have include jQuery :
<script src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
Which browser are you using?
Anyway, $(window).scrollTop(0); seems to be supported by all browsers, but if you want to animate the scroll, $('html,body').animate({scrollTop:0},500);, like you're using, if you can share your HTML and CSS code, maybe the problem is in the z-index or try to put the width and the height of the ".icon-chevron-up" also to the "#return-to-top".
Everything seems to be working, try to use the console.log('some string here'); to see if the code is running.
Also, check if you added the jquery script:
script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"></script>
Hope you solve it.
I noticed a lot of people were telling me to include the jquery library. I did add the library before but after I checked the answers I noticed that I added the wrong library. So now its fixed thanks a lot guys.
Related
quite new to coding with javascript and such, having an issue with an on-click scroll where it goes to another div. I've checked a lot of the posts on here but can't seem to find out why it won't work. Any help would be greatly appreciated! It's probably a simple fix that I just don't have the experience to be able to notice..
Here's the code (I'll try to condense it but imagine there's a lot of content in between so it's actually worth scrolling to)
HTML
<div class="button-div"><button type="button">Click here</button></div>
<div class="div-2-under-top">Content here</div>
Jscript
$("button").click(function() {
$('html,body').animate({
scrollTop: $(".div-2-under-top").offset().top},
'slow')
})
I have the latest jquery V3.3.1 so I wouldn't think that would be the issue... any ideas?
Here's a picture of the actual code itself in-case there's something important I missed (apologies if the code is dirty)
#TylerRoper found the answer for me.
I'll paste his answer here in-case anyone stumbles across this and is looking for the answer.
OP, your button click event needs to be inside of the $(function() { ... }) section. This section will be executed after waiting for the page to load. By trying to attach an event to $("button") outside of this, the button has not loaded yet, so the event can't be attached.
$("button").click(function() {
$('html,body').animate({
scrollTop: $(".div-2-under-top").offset().top},
1000);
});
Try see can work or not ? hope this help you
After combing the forums and how-to guides, I have found a solution to a Smooth Scrolling problem that I had, but I'd like to ask some kind folks if the solution below will work for me before I try it, or if I'm missing something important.
I'm working on a live site and I don't want to create problems or break anything, so I'd like to be sure before I add the code below. I also know nothing about java or coding, so please forgive me if I don't use the right terms.
I want to enable smooth scrolling to an anchor on another page.
e.g. from my home page "domain.com/home", click the link, then
load the new page, e.g. "domain.com/contact"
and on loading the new page, smoothly scroll to the anchor, "domain.com/contact#section1".
Currently, it simply jumps, and I'd like to know if the steps below will enable the smooth scrolling.
I'm planning to:
Add the following codes to the website template's '' section (in the Joomla admin panel):
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
</script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
I'm unsure whether this is necessary because I already use jQuery with some components, is it unnecessary to load jQuery again? Or will it not hurt to add this code regardless?
Then add this code to the same section in the template:
<script type="text/javascript" >
$('html').css({
display: 'none'
});
$(document).ready(function() {
var hashURL = location.hash;
if (hashURL != "" && hashURL.length > 1) {
$(window).scrollTop(0);
$('html').css({
display: 'block'
});
smoothScrollTo(hashURL);
} else {
$('html').css({
display: 'block'
});
}
});
function smoothScrollTo(anchor) {
var duration = 5000; //time
var targetY = $(anchor).offset().top;
$("html, body").animate({
"scrollTop": targetY
}, duration, 'easeInOutCubic');
}
</script>
As far as I know, this will enable the smooth scrolling, but I haven't added anything like 'smoothscroll.js' (which I've read a lot about) -- will that also need adding in the '' (after I upload it to the server), or is that included in the jQuery library?
I'm sorry if this seems very naive, I'm learning as I go. Thank you very much in advance to anyone who provides some feedback on this, I am truly grateful for your time and patience.
Best,
Ben
Firstly, Joomla already loads jQuery, so you do not need to load it again. I would either use a Joomla extension (there is a free one here) or use a smooth scroll library (like this one). Assuming you choose to do the latter, you just need to put the link in your Joomla template to the JS file and initialise it (this is all explained on the Github project page).
Both options are simple but if you don't have much experience in coding then the extension is probably the best way to go.
EDIT: To use smoothscroll on page load with the GitHub library, you will need to change your last function to:
function smoothScrollTo(anchor) {
var scroll = new SmoothScroll();
scroll.animateScroll(anchor);
}
I'm building a site where a user has to agree to terms and conditions. The checkbox is enabled after scrolling to the end of the box. Here is the code:
<script type="text/javascript">
$(function() {
$('#rules_box').scroll(function () {
if ($(this).scrollTop() >= $(this)[0].scrollHeight - $(this).height() - 15 ) {
$('#iagree').removeAttr('disabled');
}
});
});
</script>
This works in most browsers. However some refuse to work. Even if I just have
$('#iagree').removeAttr('disabled');
by itself without scroll conditions. This page is running many other javascripts that are working.
Some said it didn't work on their Mobile, and one said it didn't work on Firefox although it worked on my Firefox.
Is jQuery too finicky to work? Does this mean I have to do it another way?
EDIT:
you're quick to mark as a dupe, but this question hasn't been answered yet.
Problem persists in same select browsers, see updated code
$(function() {
$('#rules_box').scroll(function () {
alert('something');
$('#iagree').prop("disabled", false);
});
});
Does not fire alert or change checkbox state
For checkbox elements, you should use .removeProp() instead of .removeAttr()
$('#iagree').removeProp('disabled');
Also see: https://api.jquery.com/removeAttr/ and https://api.jquery.com/attr/
Please use
$('#iagree').prop("disabled", false);
Use .prop('disabled',false) instead of removeAttr('disabled').
related question
I'm quite new at this, so please be thorough with the explanation.
I'm using the Lightbox 2 jQuery file, along with another jQuery file to execute a menu slide animation and a fade animation on my images.
I'm assuming that there is conflict between the two jQuery files, but I'm not sure how to resolve it.
Any advice? I read something about jQuery.noConflict(), but I'm not sure how to implement it, or it if will work.
<script src="../Scripts/jquery-2.0.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('div#ScrollBox img').animate({
opacity:.5
});
$('div#ScrollBox img').hover(function(){
$(this).stop().animate({opacity:1}, 'fast');
}, function(){
$(this).stop().animate({opacity:.5}, 'slow');
});
});
</script>
<!--LIGHTBOX-->
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/lightbox-2.6.min.js"></script>
<link href="css/lightbox.css" rel="stylesheet" />
It looks like you are loading two different jQuery versions (1.10.2 and 2.0.2), which is, I believe, causing the problem. I would recommend removing the 1.10.2 jQuery script, and one of the following (in order of effort, in case you want to try all 3):
-see if your lightbox plugin still works
-find a newer version of the same lightbox
-use a different lightbox, for example fancybox
In any case, make sure that your end result only has one version of jQuery being loaded.
Is there a reason you need both? They should have pretty much the same code so you only need to include one. I would use whichever is the latest.
It would help if you included in your post the lines of code where you explicitly add them so we can see what you are doing.
Website test page: http://www.lantiis.com/indexold.html
jsFiddle: http://jsfiddle.net/Guhb4/7/
I received help with the jQuery and it works perfect in jsFiddle. (temp sign-in as Lantiis here: Can independently show/hide. How do I hide on show?)
When I transfered the code, everything worked except the images. The background images show up, the link images show up, but not the main images that have the site content which is what I needed the jquery for. I have no idea what I did wrong when I transferred the code over.
I am sure this is just a stupid mistake on my part, but as I am still learning js, I am at a total loss at this point. I thought I had it pretty good until I posted my first question and saw how messy my code was in comparison to the users code who helped me.
Any and all direction and criticism is welcome.
You need to include the jQuery library in your HTML.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
http://docs.jquery.com/Downloading_jQuery
And wrap your jQuery code in the following:
<script type="text/javascript">
$(document).ready(function() {
//Your code here
});
</script>
http://docs.jquery.com/How_jQuery_Works#Launching_Code_on_Document_Ready
In addition to including the jQuery library (as mentioned by Callum) and placing your code within an onload function (as mentioned by CleverQuack), your script also needs to have a type of text/javascript, not just javascript. Something like this should work:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#mask > div:first-child').show();
$('#top a').click(function() {
var keyterm = $(this).attr('title');
$('#mask > div').hide();
$('div[id=' + keyterm + ']').slideDown('slow');
});
});
</script>