can jQuery window load be pure javascript? - javascript

Can this jQuery window load script be converted into pure javascript?
I was wrong, I didn't dive into pure javascript before learning about jQuery.
can you i convert this jquery to pure javascript?
This is my code
$(window).load(function () {
$("#loading").fadeOut("fast");
});
$(window).on("beforeunload", function () {
$("#loading").fadeIn("fast").delay(1000).show();
});

The load function can be replaced with onload (scrapping the window because onload is already a global variable) and the $ jquery query selector function is the same as document.querySelector. The on function is equivalent to the addEventListener function.
The pure js code is
onload = function () {
const elem = document.querySelector("#loading");
var opacity = 1;
var timer = setInterval(() => {
opacity -= 50 / 400;
if( opacity <= 0 )
{
clearInterval(timer);
opacity = 0;
elem.style.display = "none";
elem.style.visibility = "hidden";
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50); // this part is from https://stackoverflow.com/questions/13733912/javascript-fade-in-fade-out-without-jquery-and-css3
};
addEventListener("beforeunload", function () {
const elem = document.querySelector("#loading");
elem.style.opacity = 0;
elem.style.filter = "alpha(opacity=0)";
elem.style.display = "inline-block";
elem.style.visibility = "visible";
var opacity = 0;
var timer = setInterval( function() {
opacity += 50 / 400;
if( opacity >= 1 )
{
clearInterval(timer);
opacity = 1;
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50 ); // this part is also from https://stackoverflow.com/questions/13733912/javascript-fade-in-fade-out-without-jquery-and-css3
setTimeout(()=>{elem.style.display="block";},1000);
});
this all should match up to what jquery would do but something could always go wrong.

Related

Implementing FadeOut/FadeIn in pure JavaScript

My problem is that only fadeOut works but not fadeIn. I don't use CSS or jQuery for it. And how can I make my code more effective.
The code:
HTML:
<div class="circle"id="circle1"onclick="myFunction()"></div>
<div class="circle"id="circle2" style="visibility:visible" ></div
JavaScript:
function myFunction() {
var element = document.getElementById("circle2");
if (element.style.visibility === "visible") {
var op = 1;
var timer = setInterval(frame, 100)
function frame() {
if (op <= 0.1) {
clearInterval(timer);
element.style.visibility = "hidden";
}
element.style.opacity = op;
op -= op * 0.1;
}
} else {
var op = 0;
var timer = setInterval(frame, 10)
function frame() {
if (op >= 0.95) {
clearInterval(timer);
element.style.visibility = "visible";
}
element.style.opacity = op;
op += op * 0.1;
}
}
}
There are a few reasons this is not working.
0 * anything is still 0 so var op=0; and op += op * 0.1 will always be zero.
It is set to hidden not visible while transitioning to fully visible.
I played around with it to make it work but I refactored you code to fit my style while I did it. This is what I came up with.
function myFunction() {
var element = document.getElementById( "circle2" );
var op;
var timer;
if ( element.style.visibility === "visible" ) {
op = 1;
timer = setInterval( fadeOut, 100 )
} else {
op = 0.1;
timer = setInterval( fadeIn, 100 )
}
function fadeOut() {
if ( op <= 0.1 ) {
clearInterval( timer );
element.style.visibility = "hidden";
}
element.style.opacity = op;
op -= op * 0.1;
}
function fadeIn() {
element.style.visibility = "visible";
if ( op >= 0.95 ) {
clearInterval( timer );
}
element.style.opacity = op;
op += op * 0.1;
}
}
<div class="circle"id="circle1"onclick="myFunction()">hello</div>
<div class="circle"id="circle2" style="visibility:visible" >Test</div>

Animate text change in block with fade-in and fade-out

I have a div which content is changed dynamically by a script and I wanna add fade-in and fade-out animation on text when it's being changed. How can I do it using CSS or pure JS? All the solutions I've seen so far involved jQuery, while I'm interested in pure CSS and/or JS.
Fade Out:
var fadeout = function(elem) {
var o = 1;
var timer = setInterval(function () {
if (o <= 0.0) {
clearInterval(timer);
}
elem.style.opacity = o;
elem.style.filter = 'alpha(opacity=' + o * 100 + ")";
o -= 0.1;
}, 25);
};
Fade In:
var fadein = function(elem) {
var o = 0;
var timer = setInterval(function () {
if (o >= 1.0) {
clearInterval(timer);
}
elem.style.opacity = o;
elem.style.filter = 'alpha(opacity=' + o * 100 + ")";
o += 0.1;
}, 25);
};
Also this fiddle seems amazing:
http://jsfiddle.net/gabrieleromanato/cMp7s/
Pure CSS:
http://fvsch.com/code/transition-fade/test5.html

Scroll down animation js

I built a kind of chat in JS and then I wanted that when I got new message the chat automatically scrolled down (with animation...). Everything worked beautifully, but after the animation stopped the user couldn't scroll by himself; the chat automatically scrolled to the end.
So this is the code :
<!-- language:lang-js -->
var height = 1;
window.setInterval(function() {
var elem = document.getElementById('chat');
elem.scrollTop = height;
if (elem.scrollheight < height) {
clearInterval(this);
}
height += 2;
}, 50);
the clearInterval function expects a number. Using that should make it work correctly. You also have many syntax errors.
var intervalReference = window.setInterval(function() {
var elem = document.getElementById('chat');
elem.scrollTop = height;
if (elem.scrollHeight < height) {
clearInterval(intervalReference);
}
height += 2;
}, 50);
you should make a var holding the interval like this :
var height = 1;
var interval = window.setInterval( animate, 50 );
function animate() {
var elem = document.getElementById('chat');
elem.scrollTop = height;
if (elem.scrollHeight < height) {
clearInterval( interval );
}
height += 2;
}
this should work fine

How to stop a javascript process by another event?

Consider we have a simple cycling Javascript process as:
function test() {
el=document.getElementById("test");
var opacity = 1;
var id = setInterval(function() {
opacity = opacity - 0.1;
el.style.opacity= opacity;
\\ if(mouseout) {clearInterval(id);} How to do this?
if(opacity == 0) {clearInterval(id);}
}, 500);
}
document.getElementById("test").
addEventListener('mouseover', function(){
test();
});
Upon moveover event, the process initiates and continues until reaching the if condition. How we can define another if condition to stop the process by another event.
In the current example, how we can stop the process (reducing the opacity) upon mouseout event.
Declare your id variable outside the function. Then you can call clearInterval(id) from your mouseout handler.
Note that you don't really need the test() function, you can put its contents directly in your mouseover handler:
var id,
el = document.getElementById("test");
el.addEventListener('mouseover', function(){
var opacity = 1;
id = setInterval(function() {
opacity = opacity - 0.1;
el.style.opacity= opacity;
if(opacity == 0) {clearInterval(id);}
}, 500);
});
el.addEventListener('mouseout', function() {
clearInterval(id);
});
var el = document.getElementById("test"),
opacity = 1,
id;
el.addEventListener('mouseover', function(){
id = setInterval(function() {
opacity = opacity - 0.1;
el.style.opacity= opacity;
if(opacity == 0) {clearInterval(id);}
}, 500);
});
el.addEventListener("mouseout", function() {
clearInterval(id);
});

javascript toggle animation issue

I am doing a small javascript animation. this is my code :
window.onload = function () {
var heading = document.getElementsByTagName('h1')[0];
heading.onclick = function () {
var divHeight = 250;
var speed = 10;
var myInterval = 0;
alert(divHeight);
slide();
function slide() {
if (divHeight == 250) {
myInterval = setInterval(slideUp, 30);
} else {
myInterval = setInterval(slideDwn, 30);
alert('i am called as slide down')
}
}
function slideUp() {
var anima = document.getElementById('anima');
if (divHeight <= 0) {
divHeight = 0;
anima.style.height = '0px';
clearInterval(myInterval);
} else {
divHeight -= speed;
if (divHeight < 0) divHeight = 0;
anima.style.height = divHeight + 'px';
}
}
function slideDwn() {
var anima = document.getElementById('anima');
if (divHeight >= 250) {
divHeight = 250;
clearInterval(myInterval);
} else {
divHeight += speed;
anima.style.height = divHeight + 'px';
}
}
}
}
i am using above code for simple animation. i need to get the result 250 on the first click, as well second click i has to get 0 value. but it showing the 250 with unchanged. but i am assigning the value to set '0', once the div height reached to '0'.
what is the issue with my code? any one help me?
Everytime you click on the div the divHeight variable is reset to 250, thus your code never calls slideDwn. Moving the divHeight declaration outside the event handler should do the trick.
Also, your div wont have the correct size when any of the 2 animations end. You're setting the divHeight variable to 250 or 0 correctly, but never actually setting anima.style.height after that.
I've rewritten your code into something simpler and lighter. The main difference here is that we're using a single slide() function here, and that the height of the div in question is stored in a variable beforehand to ensure that the element slides into the correct position.
Note that this is a very simplistic implementation and assumes that the div carries no padding. (The code uses ele.clientHeight and ele.style.height interchangeably, which admittedly, is a pretty bad choice, but is done here to keep the code simple)
var heading = document.getElementsByTagName('h1')[0],
anima = document.getElementById('anima'),
divHeight = anima.clientHeight,
speed = 10,
myInterval = 0,
animating = false;
function slide(speed, goal) {
if(Math.abs(anima.clientHeight - goal) <= speed){
anima.style.height = goal + 'px';
animating = false;
clearInterval(myInterval);
} else if(anima.clientHeight - goal > 0){
anima.style.height = (anima.clientHeight - speed) + 'px';
} else {
anima.style.height = (anima.clientHeight + speed) + 'px';
}
}
heading.onclick = function() {
if(!animating) {
animating = true;
var goal = (anima.clientHeight >= divHeight) ? 0 : divHeight;
myInterval = setInterval(slide, 13, speed, goal);
}
}
See http://www.jsfiddle.net/yijiang/dWJgG/2/ for a simple demo.
I've corrected your code a bit (See working demo)
window.onload = function () {
var heading = document.getElementsByTagName('h1')[0];
var anima = document.getElementById('anima');
var divHeight = 250;
heading.onclick = function () {
var speed = 10;
var myInterval = 0;
function slideUp() {
divHeight -= speed;
if (divHeight <= 0) {
divHeight = 0;
clearInterval(myInterval);
}
anima.style.height = divHeight + 'px';
}
function slideDwn() {
divHeight += speed;
if (divHeight >= 250) {
divHeight = 250;
clearInterval(myInterval);
}
anima.style.height = divHeight + 'px';
}
function slide() {
console.log(divHeight )
if (divHeight == 250) {
myInterval = setInterval(slideUp, 30);
} else {
myInterval = setInterval(slideDwn, 30);
}
}
slide();
}
}​

Categories