I'm simply tring to make changes to my webpage by scrolling the page. And in this code I am trying to change the background color of a div that is fixed in page. What is wrong with this code?
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">
body{
height: 5000px;
}
div{
height: 100px;
width: 100px;
background: red;
position: fixed;
top: 300px;
}
</style>
</head>
<body>
<div></div>
<script type="text/javascript">
function change(){
if( $(window).scrollTop() > 500 ){
$('div').css("background", "green");
} else{
$('div').css("background", "red");
}
}
change();
</script>
</body>
</html>
i think you should call it in .scroll() method:
$(function(){
$(window).on('scroll', function(){
change();
}).scroll();
});
or you can do shorten the code like this:
$(function(){
$(window).on('scroll', change).scroll();
});
Issue in your code is that you just called your function and that point of time the .scrollTop() value was 0 and it never gets updated.
so the solution is to put this function in an event of .scroll() the way suggested above. At every scroll it gets the new scrolltop position.
<script type="text/javascript">
$(document).scroll(function(){
if ($(window).scrollTop() > 500) {
$('div').css("background", "green");
} else {
$('div').css("background", "red");
}
});
</script>
Please try place your change within the document.ready..
other than that it looks OK.
<script type="text/javascript">
function change(){
if( $(window).scrollTop() > 500 ){
$('div').css("background", "green");
} else{
$('div').css("background", "red");
}
}
$(document).ready(function() {
change();
});
</script>
Instead of accessing $(window).scrollTop use $(document).scrollTop.
JQUERY CODE:
$(document).on('scroll',function () {
if( $(this).scrollTop() > 500 ){
$('aside').css("background", "green");
} else{
$('aside').css("background", "red");
}
});
$(document).scroll();
LIVE DEMO:
http://jsfiddle.net/dreamweiver/PBG9Z/15/
Happy Coding :)
Related
I'm trying to use javascript to toggle the size of a div. In order to understand the basic idea of how to do this I tried replicating this example: Toggling Div (JSFiddle)
But for some reason it is not working, despite me having copied it from the working JSFiddle. Why is that? Here is my replicate:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style type="text/css">#topbar {
background: orange;
color: white;
height: 10px;
text-align:center;
}</style>
<script type="text/javascript">
$("#topbar").click((function() {
var i = 0;
return function() {
$(this).animate({
height: (++i % 2) ? 40 : 10
}, 200);
}
})());
</script>
</head>
<body>
<div id='topbar'>toggle me</div>
</body>
</html>
Wow! You haven't added jQuery at all! Add this in your <head> before you call the script.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You must have got this error in the console:
Reference Error: $ not defined.
You are using this not in context. When you put it inside a function, then it gets screwed up. Do it this way:
$(function () {
$("#topbar").click(function () {
var i = 0;
$(this).animate({
height: (++i % 2) ? 40 : 10
}, 200);
return false;
});
});
The this you used, will be taking your inner function as its context. And since your <div> comes after the <script>, please enclose it inside the $(function () {}).
I have 3 html documents: header.html, blackPage.html, and greenPage.html. header.html is common to both blackPage.html and greenPage.html. I want to make it so that a button in the header, used to change pages, reflects the color of the current page using a script called HeaderScript.js. How should I go about doing this? (This is just an example: in the real application, the green page won't have enough info on it to determine what the header should be, so I can't just update it when I load the green page).
Here are the 4 files I'm using:
header.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src=HeaderScript.js></script>
<script>
$(document).ready(function(){
$("#Button").click(function(){
changePage($("#Button").get(0));
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
background-color: black;
}
</style>
<div id="Button"></div>
</html>
blackPage.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#Header").load("header.html");
});
</script>
<Title>Black Page</Title>
<h>Black Page</h>
<div id="Header"></div>
</html>
greenPage.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#Header").load("header.html");
});
</script>
<Title>Green Page</Title>
<h>Green Page</h>
<div id="Header"></div>
</html>
HeaderScript.js
function changePage(div){
if (div.style.backgroundColor === "green"){
div.style.backgroundColor = "black";
alert("Color changed to black");
document.location.href = "blackPage.html";
}
else{
div.style.backgroundColor = "green";
alert("Color changed to green");
document.location.href = "greenPage.html";
}
}
New header.html page code:
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
<script src="HeaderScript.js"></script>
<script>
$(document).ready(function(){
var path = window.location.pathname;
path = path.substr(path.lastIndexOf('/') + 1,
path.indexOf('Page.html') - path.lastIndexOf('/') - 1);
$("#Button").css('background-color', path);
$("#Button").click(function(){
path = (path == 'green' ? 'black' : 'green');
$("#Button").css('background-color', path);
window.location = path + 'Page.html';
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
}
</style>
<div id="Button"></div>
</html>
Hope that works ;)
Let me know..
ThatWeirdo's answer works, and his answer makes the color load before the page changes (as opposed to this solution), but I need to do it differently for the specific problem for which this question was just an example (my original code was garbage, so I made this example to try to make it simpler/clearer).
Here is what I'm going to end up doing (this assumes blackPage.html is always the first page loaded):
header.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src=HeaderScript.js></script>
<script>
$(document).ready(function(){
//--ADDED--
var buttonColor = localStorage.buttonColor;
if(buttonColor !== undefined){
$("#Button").css("background-color", buttonColor);
}
//----
$("#Button").click(function(){
changePage($("#Button").get(0));
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
background-color: black;
}
</style>
<div id="Button"></div>
</html>
HeaderScript.js
function changePage(div){
if (localStorage.buttonColor === "green"){ //CHANGED
localStorage.buttonColor = "black"; //CHANGED
alert("Color changed to black");
document.location.href = "blackPage.html";
}
else{
localStorage.buttonColor = "green"; //CHANGED
alert("Color changed to green");
document.location.href = "greenPage.html";
}
}
I am not having any luck getting jqueryrotate example #2 on this page to work on a simple html page. What am I doing wrong? Thanks for the help.
Here is my code -
<!DOCTYPE html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="http://jqueryrotate.googlecode.com/svn/trunk/jQueryRotate.js"></script>
<STYLE type="text/css">
#img { margin:100px 100px;}
</STYLE>
</head>
<body>
<script type="text/javascript">
$("#img").rotate({
bind:
{
mouseover : function() {
$(this).rotate({animateTo:180})
},
mouseout : function() {
$(this).rotate({animateTo:0})
}
}
});
</script>
<img id="img" src="https://www.google.com/images/srpr/logo3w.png">
</body>
</html>
you code works just fine, see here.
You should probably use ready.
$(document).ready(function() {
$("#img").rotate({
bind: {
mouseover: function() {
$(this).rotate({
animateTo: 180
})
},
mouseout: function() {
$(this).rotate({
animateTo: 0
})
}
}
});
});
Also, I would not recommend using the id #img it's not that it's wrong, it's just sort of ugly IMO.
I have a Bee image and I want to animate it using jQuery.
The idea is to move the image from left (outside of screen) to right (outside of screen) to create an effect like it's flying.
Your bee needs to be absolutely positioned, something like this:
<div id="b" style="position:absolute; top:50px">B</div>
I've used a div here, but it could just as well be an <img> tag. As meo pointed out, don't forget the top attribute, because some browsers don't work without it. Then you can animate it:
$(document).ready(function() {
$("#b").animate({left: "+=500"}, 2000);
$("#b").animate({left: "-=300"}, 1000);
});
Here is a jsfiddle demo.
If you want to have a continuous animation as Hira pointed out, put the animation code in functions, make sure the left and right movement is the same, and use the onComplete option of animate() to call the next animation:
function beeLeft() {
$("#b").animate({left: "-=500"}, 2000, "swing", beeRight);
}
function beeRight() {
$("#b").animate({left: "+=500"}, 2000, "swing", beeLeft);
}
beeRight();
And the fiddle for that.
Try spritely: http://spritely.net/
i would do something like this:
http://jsfiddle.net/Uwuwj/2/
var b = function($b,speed){
var beeWidth = $b.width();
$b.animate({ //animates the bee to the right side of the screen
"left": "100%"
}, speed, function(){ //when finished it goes back to the left side
$b.animate({
"left": 0 - beeWidth + "px"
}, speed, function(){
b($b, speed) //finally it recalls the same function and everything starts again
});
});
};
$(function(){ //document ready
b($("#b"), 5000); //calls the function
});
bee careful, this code only works with bee's :P
In case you want the bee to keep flying across the screen, try this :-)
<html>
<head>
<script type="text/javascript" src="jquery/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function animateImage() {
console.log("Called");
$('#bee').css({right:'10%'});
$('#bee').animate({right: '-100%'}, 5000, 'linear', function(){animateImage();});
}
$(document).ready(function() {
animateImage();
});
</script>
</head>
<body>
<div style="width: 100%;"><img src="bee.jpg" id="bee" style="position:relative;"/></div>
</body>
<script type="text/javascript" src="jquery/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
function rgt() {
$('#sldr').animate({ left: "500" }, 10000, hider);
}
rgt();
function hider() {
$('#sldr').css('left', '0px');
rgt();
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="sldr" src="../Images/animated%20images/birds/rightfuglan.gif" style="position:absolute" />
</div>
</form>
</body
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="../Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
var k = $(window).width();
function rgt() {
$('#sldl').hide(1);
$('#sldr').animate({ left: "1000" }, 10000, hider);
}
rgt();
function hider() {
$('#sldr').css('left', '0px');
$('#sldr').hide(1);
$('#sldl').show();
lft();
}
function lft() {
$('#sldl').animate({ left: "0" }, 10000, hidel);
}
function hidel() {
$('#sldl').css('left', '1000px');
$('#sldr').show();
rgt();
}
});
</script>
<style type="text/css">
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="sldl" src="../Images/animated%20images/birds/fuglan.gif" style="position:absolute; right:0px" />
<img id="sldr" src="../Images/animated%20images/birds/rightfuglan.gif" style="position:absolute" />
</div>
</form>
</body>`enter code here`
basically I'm trying to get #toggle_box to slide down using Jquery, the code below works if I'm just using document.body as the selector however it doesn't seem to work when #toggle is the selector! Any ideas why?
<html>
<head>
<title>JQuery Testage</title>
<style>
#toggle_box {
display: none;
}
#toggle {
background-color: #FFF000;
border: solid;
}
</style>
</head>
<body>
<script src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$("#toggle").click(function () {
if ($("#toggle_box").is(":hidden")) {
$("#toggle_box").slideDown("slow");
} else {
$("#toggle_box").hide();
}
});
</script>
<div id="toggle">Toggle</div>
<div id="toggle_box">This should silde using the Jquery library 1.4.2</div>
</body>
</html>
Give the click handler a $(document).ready(... wrap, and it should work:
$(document).ready(function() {
// your click handler and anything else that
// should run when the DOM is ready
});
Modify your code thus:
<script type="text/javascript">
$(document).ready(function() {
$("#toggle").click(function () {
if ($("#toggle_box").is(":hidden")) {
$("#toggle_box").slideDown("slow");
} else {
$("#toggle_box").hide();
}
});
});
</script>
The problem is that the javascript is loaded and executed before the toggle_box element is loaded. It works for the document body as that is loaded before the javascript.
Could it be because the DOM isn't ready yet?
Try wrapping your script in the following piece of code:
$(function() {
// your code here
});
That's a short code for $(document).ready(function(){ ...}); and may just be what's missing?