How To Make A Lightbox Animation Effect Only Using Javascript (no library) - javascript

I am just new to Javascript. I want to implement lightbox effect without using jQuery. I can successfully add a fixed div with 100% height and width of body element by using Javascript but there is no animation effect.
HTML:
<html>
<head>
<title>Lightbox Example</title>
<link rel="stylesheet" href="lightboxstyle.css" type="text/css">
</head>
<body>
<ul>
<li><img class="preview" src="images/blue.jpg" title="blue"></li>
<li><img class="preview" src="images/red.jpg" title="red"></li>
<li><img class="preview" src="images/yellow.jpg" title="yellow"></li>
<li><img class="preview" src="images/green.jpg" title="green"></li>
</ul>
<script src="lightbox.js"></script>
</body>
</html>
Stlesheet
.preview {list-style: none;display:inline;width:200px;}
ul li {display:inline;margin-left:1%}
body {background-color:#eee;}
#lightbox {
position:fixed;
left:0;
top:0;
z-index: 999;
background-image:url(images/1.png);
}
Javascript
function lightboxboot(){
var lightbox=document.createElement("div")
lightbox.id="lightbox"
var ul=document.getElementsByTagName("ul")
ul[0].insertBefore(lightbox)
for (var x=1;x<100;x++){
setTimeout(function(){
lightbox.style.width=x+"%";
lightbox.style.height=x+"%";
},50)
}
}
var image=document.getElementsByClassName("preview")
for (var i=0;i<image.length;i++){
image[i].onclick=lightboxboot;
}
The lightbox div can still cover the entire body element after the script runs over but these is no animation effect.

for (var x=1;x<100;x++){
setTimeout(function(){
lightbox.style.width=x+"%";
lightbox.style.height=x+"%";
},50)
}
Is wrong. You set a bunch of timeouts for the same time and try to access x wrongly. Here:
for (var x=1;x<100;x++){
setTimeout(function(x){
lightbox.style.width=x+"%";
lightbox.style.height=x+"%";
},x*50,x)
}

Related

How do I add transition/animation to slider of images?

How to add a transition effect when the image changes?
When the array loops through images, how do I add a transition so that its more smooth.
var i = 0;
var images = [];
var time = 3000;
images[0] = 'test.jpg';
images[1] = 'test1.jpg';
images[2] = 'test2.jpg';
function changeImg() {
document.slide.src = images[i];
if(i < images.length - 1) {
i++
} else {
i = 0;
}
setTimeout("changeImg()", time);
}
window.onload = changeImg;
My easiest way to do it is putting all the images into 1 wrapper.
Then use loop to set active image by element index.
Something like this
<style>
.wrapper { position: relative; }
.wrapper img {
opacity: 0;
visibility: hidden;
transition: 0.5s opacity;
position: absolute;
}
.wrapper img.active {
opacity: 1;
visibility: visible;
position: relative;
}
</style>
<div class="wrapper">
<img src={images[0]} />
<img src={images[1]} />
<img src={images[2]} />
<img src={images[3]} />
</div>
What you are asking for is called cross-fading, this is a possible duplicate of How can I smoothly transition CSS background images?
But a more detailed guide that worked for me in the past can be found here.
http://css3.bradshawenterprises.com/cfimg/
Keep in mind that you might have to take browser compatibility into consideration if you go the CSS route. Properties such as the -webkit CSS extension is just one of many.
One of the easiest ways I found is using w3.css.
In your html code add these lines.
<html>
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<img class="w3-animate-fading" name="slide" src="" width="800" />
</body>
</html>
The class parameter in your image tag will call the predefined css animations deom w3.css file.
There are many more options such as slide right, slide left etc. You can view them all here
Codepen Demo

Where did I go wrong with my javascript handling my slideshow image banner?

Here's what I have so far, it's a very basic slide image banner inside my header tag in html. Basically, every 3 seconds, I have a new image showing up. I plan on using that for the mock exercise I'm working on right now but unfortunately, something went wrong in Javascript. At least, that's what I think it is. The image banner does NOT keep going through the images. It just sticks to one picture and goes no where else. No other pictures show up. This is the error I'm getting inside my console log right now.
Uncaught TypeError: Cannot read property 'setAttribute' of null. at changeImage (slideshow.js:7)
Here's the HTML I have right now.
<html>
<head>
<title>Kenneth's Slide Show Demonstration</title>
<meta charset="UTF-8">
<link rel='stylesheet' type='text/css' href='slideshow.css'>
<script src="slideshow.js"></script>
</head>
<body>
<div id="wallpaper"><img src="ferrari-logo0.jpg" id="ferrari" alt="ferrari logo"/></div>
</body>
</html>
now for CSS.
body {
padding:0;
margin:0;
}
#wallpaper {
height:300px;
width:100%;
margin:auto;
background:rgba(0,0,0,0.7);
border-bottom:dashed black 1px;
transition:all 0.5s ease-in-out;
}
#ferrari {
margin-left:auto;
margin-right:auto;
display:block;
height:250px;
width:400px;
padding-top:20px;
}
#wallpaper:hover {
background:rgba(0,0,0,1);
}
And then here's the Javascript I've made.
var myImage = document.getElementById('ferrari');
var imageArray=['ferrari-logo0.jpg', 'ferrari-logo1.jpg', 'Ferrari-logo2', 'ferrari-logo3.jpg'];
var imageIndex=0;
function changeImage() {
myImage.setAttribute("src", imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
};
var intervalHandle = setInterval(changeImage(),4000);
function changeImage();
Where did I go wrong? If I can make a special request, I'd like straight Vanilla javascript. I know Jquery is popular but I'm not currently studying that right now.
No need in round brackets in setInterval for function changeImage();
var imageArray = ['ferrari-logo0.jpg', 'ferrari-logo1.jpg', 'Ferrari-logo2', 'ferrari-logo3.jpg'];
var imageIndex = 0;
function changeImage() {
var myImage = document.getElementById('ferrari');
if (myImage) {
myImage.setAttribute("src", imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
}
};
var intervalHandle = setInterval(changeImage, 4000);
Try it. You must pass a function, not invoke it.
You are loading your JavaScript in the head of your page, so when your JS runs, the ferrari element does not exist yet. getElementById returns null if it doesn't find an element with the id you provide. Thus, you are in effect trying to do null.setAttribute("src", imageArray[imageIndex]);. Since null doesn't have a setAttribute method you are getting the error message telling you Cannot read property 'setAttribute' of null.
Moving your script tag below the ferrari element should fix it. It has become a best practice to load your JS as low in the page as you can for other performance related reasons. You could also keep your script tag where it is if you wrap your code in a DOMContentLoaded event handler.
You just need to invoke changeImage, like this setInterval(changeImage,4000) also changeImage() is already defined, you just need to call it.
Here is a working example.
document.addEventListener("DOMContentLoaded", function() {
var myImage = document.getElementById('ferrari');
var imageArray=['http://placehold.it/350x150?text=1',
'http://placehold.it/350x150?text=2',
'http://placehold.it/350x150?text=3',
'http://placehold.it/350x150?text=4'
];
var imageIndex=0;
function changeImage() {
myImage.setAttribute("src", imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex = 0;
}
}
var intervalHandle = setInterval(changeImage, 4000);
changeImage();
});
body {
padding:0;
margin:0;
}
#wallpaper {
height:300px;
width:100%;
margin:auto;
background:rgba(0,0,0,0.7);
border-bottom:dashed black 1px;
transition:all 0.5s ease-in-out;
}
#ferrari {
margin-left:auto;
margin-right:auto;
display:block;
height:250px;
width:400px;
padding-top:20px;
}
#wallpaper:hover {
background:rgba(0,0,0,1);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Wallpapers</title>
</head>
<body>
<div id="wallpaper">
<img src="" id="ferrari" alt="ferrari logo"/></div>
</body>
</html>
It's best practice to load your scripts after html is loaded, so place your html before then end body tag </body>, or use DOMContentLoaded https://developer.mozilla.org/en/docs/Web/Events/DOMContentLoaded

an effect onto gallery of images using same javascript

i have the following code using which i implement effects on an image using javascript.
the effect is like when an user hovers on the main image then the over1 and over2 are the two images which gets overlayed on it providing decent information.
now my problem is-- the code i got is for only single images.
when i am implementing it on gallery of images then even i hover on only one image still all images get overlayed with their information.
i need help regarding that if i hover on then other images shouldn't be displaying their info only one should be active.
i also need to know how should i store my images in folders. so far i had decided to make one for main images and other for overlaying images.
my code is
<html>
<head>
<link href="css/overlaycss.css" rel="stylesheet" />
<script src="js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".main").mouseenter(function() {
$(".overlay").show();
});
$(".main").mouseleave(function() {
$(".overlay").hide();
});
$(".main").mouseenter(function() {
$(".overly").show();
});
$(".main").mouseleave(function() {
$(".overly").hide();
});
});
</script>
</head>
<body style="margin:0px; padding-top:0px">
<ul>
<li>
<a href="">
<img class="main" src="image/productold.JPG" />
<img class="overlay" src="image/overlay/over1.jpg"/>
<img class="overly" src="image/overlay/over2.jpg"/>
</a>
</li>
<li>
<a href="">
<img class="main" src="image/productold.JPG" />
<img class="overlay" src="image/overlay/over1.jpg"/>
<img class="overly" src="image/overlay/over2.jpg"/>
</a>
</li>
</ul>
</body>
</html>
css is--
li{
position:relative;
float:left;
padding-left:5px;
}
.main {
width:200px;
height:200px;
}
.overlay
{
position:absolute;
height:50px;
width:150px;
top:0;
left:0;
display:none;
}
.overly
{
position:absolute;
height:50px;
width:150px;
bottom:0;
right:0;
display:none;
}
For displaying hover images on individual images try this:-
$(document).ready(function() {
$("ul li").mouseenter(function() {
$(this).find(".overlay").show();
$(this).find(".overly").show();
});
$("ul li").mouseleave(function() {
$(this).find(".overlay").hide();
$(this).find(".overly").hide();
});
});

jQuery and Js functions syntax mixing

I have small problem with the following. This is ofc just a snippet of the whole code and it is usually run through a setInterval(time,function); command but I'd like to replace the auto-sliding pictures by having instead a "next" button, why can't I just use jQuery and stick it into a
$("#nextBtn").click(nextSlide);
command ? I get my button appearing but no event. Could it be that I'm putting the jQuery command in a JS
function onLoadWindow(e) {}
instead of jQuery's
$(document).ready(function() {})
I just finished learning the basics of JS recently and started jQ shortly after, so I'm still a beginner in both but with some prior programming experience with Java. I'm kinda new to mixing syntax's together. Thanks a bunch for the help! =)
EDIT : replaced code snippet with full code. I tried skimming it by taking out what wasn't needed like a couple tags and relevant styling. But now it seems I can't even get the div button to send text to console so Im pretty sure there's an obvious "noob" error somewhere, sorry for "wasting" people's time ..
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS Slideshow</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<style type="text/css">
.slide {
height:200px;
width:320px;
position:absolute;
opacity:0;
}
img {
width:100%;
height:100%;
}
#slideshow {
position:relative;
}
.active {
opacity:1;
transition:opacity 1s;
}
#nextBtn {
display:block;
float:left;
height:25px;
width:40px;
background-color:black;
margin-top:210px;
}
</style>
<script type="text/javascript">
window.addEventListener("load",onLoadWindow);
var active_slide;
var slides;
function onLoadWindow(e) {
var slideShow=document.getElementById("slideshow");
slides=slideShow.getElementsByTagName("div");
active_slide=0;
slides[0].classList.add("active");
//setInterval(nextSlide,10000);
$("nextBtn").click(nextSlide);
}
function nextSlide () {
slides[active_slide].classList.remove("active");
active_slide++;
active_slide%=3;
slides[active_slide].classList.add("active");
}
</script>
</head>
<body>
<div id="slideshow">
<div class="slide">
<img src="IMG/bridge.jpg" alt="" title="" />
</div>
<div class="slide">
<img src="IMG/leaf.jpg" alt="" title="" />
</div>
<div class="slide">
<img src="IMG/road.jpg" alt="" title="" />
</div>
</div>
<div id="nextBtn"></div>
</body>
</html>
it was indeed just a missing hash symbol to correctly refer to the Id .. thanks to all and of course VeXii for pointing it out x)

Change image on mouseover for a button on another div

On the "home" page I want to have a logotype and a menu on a #banner div (which will then be there throughout the whole site) and on a #content" div to have an image. All these divs are inside a #container" div. The menu has 3 buttons.
I would like that on mouseover event each button displayed image on the #content div changes accordingly. So basically, when hover button1, the image on #content will change from background.jpg to background1.jpg. The event of mouseover on button2 will change it to background2.jpg etc. When buttons are not hovered over, the image should revert to the original background.jpg.
HTML:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>E.S.T.</title>
<link href="_css/layout.css" rel="stylesheet" type="text/css">
<link href="SpryAssets/SpryMenuBarHorizontal.css"
rel="stylesheet"
type="text/css">
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="banner">
<div id="logo">E.S.T.</div>
<div id="menu">
<ul id="MenuBar1" class="MenuBarHorizontal">
<li id="button1">Biography</li>
<li id="button2">Albums</li>
<li id="button3">Links</li>
</ul>
</div>
</div>
<div id="content">
<img id="back0" src="_img/background.jpg">
<img id="back1" src="_img/back_bio.jpg">
</div>
</div>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1,
{
imgDown:"SpryAssets/SpryMenuBarDownHover.gif",
imgRight:"SpryAssets/SpryMenuBarRightHover.gif"
});
</script>
</body>
</html>
CSS:
#charset "UTF-8";
#import url("../_fonts/Days/fontstylesheet.css");
body {
background-color:#CCC;
font-family:Days;
font-size:100%;
}
#container {
width:850px;
max-height: 650px;
margin: 0 auto;
padding-left: 10px;
padding-right: 10px;
overflow: hidden;
font-family: Days;
}
#logo {
position:relative;
font-size: 4em;
color:white;
float:left;
}
#menu {
float:right;
margin-top:40px;
}
I have tried several different things but I manage only to change the background image from the buttons themselves. From searching around the web i think this should be done with JS, but i have no idea how to do it.
This can be solved entirely with CSS, but first let me give you a tip:
Combine background.jpg and background1.jpg into one image, and rather change the background position. This way, there won't be any delay from when the user hovers over the menu element to when the picture is displayed, and you'll have fewer files to keep track of.
Say we let #button1 be 100px tall. We make an image 200px tall containing the normal state image on top, and the hover image on the bottom. This is called a sprite.
#button1 {
height: 100px;
background-image: url("background.jpg");
}
#button1:hover {
background-position: 0 -100px;
}
This moves the background image, showing the hover version.
For convenience, I'll answer this question using the jQuery javascript library.
If I understand you right, you would like #content to contain an image that changes when you hover over the menu items, and the image should reflect the item currently hovered.
In stead of including every image in the body, I'll try an approach using the data attributes.
HTML The relevant parts
<ul id="MenuBar1" class="MenuBarHorizontal">
<li id="button1" data-img="background.jpg">Biography</li>
<li id="button2" data-img="back_album.jpg">Albums</li>
<li id="button3">Links</li>
</ul>
<div id="content">
<img id="back"
src="_img/background.jpg"
data-original="_img/background.jpg"
alt="e.s.t" />
</div>
JavaScript
$(document).ready(function() {
$("#MenuBar1 li").mouseover(function() {
$("#back").attr("src", $(this).data("img"));
}).mouseout(function() {
$("#back").attr("src", $("#back").data("original"));
});
});​
So now we store the original image path with the image tag in its data-original attribute, and the path to the :hover image is stored with the menu element.
See this Fiddle for a demo!
Give an id on your image like: id=idimage
You can use jQuery like this:
<script type="text/javascript">
$(document).ready(function(){
$("#MenuBar1 li").mouseover(function(){
var id=$(this).attr('id');
var number = id[id.length-1];
$("#id_image").attr("src","_img/background"+number+".jpg");
});
});
</script>

Categories

Resources