Randomize Image Fade In - javascript

can anyone help me about my problem.
i have a 5 images (which is set in a specific position) with the use of bootstrap then i want it to have an animation to be specific a FADE animation on page load here's my mark up and js and its working properly but I want my image to show randomly can anyone help me.
$(function () {
$('#fds img').each(function (i) {
$(this).delay((i++) * 1000).fadeTo(2000, 1);
})
});
<div id="fds">
<div class="col-md-12 margin-b-2" style="padding-left: 0; padding-top: 20px">
<div class="com-sm-6 col-md-3">
<img id="img1" class="thumbnail" src="../img/1.jpg" alt="Alternate Text" height="300" width="330" onclick="javascript:modalImg1();" />
</div>
<div class="com-sm-6 col-md-3">
<img id="img2" class="thumbnail" src="../img/2.jpg" alt="Alternate Text" height="300" width="330" onclick="javascript:modalImg2();" />
</div>
<div class="com-sm-6 col-md-3">
<img id="img3" class="thumbnail" src="../img/3.jpg" alt="Alternate Text" height="300" width="330" onclick="javascript:modalImg3();" />
</div>
<div class="com-sm-6 col-md-3">
<img id="img4" class="thumbnail" src="../img/4.jpg" alt="Alternate Text" height="300" width="330" onclick="javascript:modalImg4();" />
</div>
</div>
<div class="col-md-12">
<img id="img5" class="thumbnail" src="../img/5.png" alt="Alternate Text" style="padding-left: 10px; padding-right: 10px" height="300" width="1350" onclick="javascript:modalImg5();" />
</div>
</div>

$(function() {
function Shuffle(o) {
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}; //stole...borrowed from CSS tricks!
rand = [];
$('#fds img').each(function(i) {
rand.push(i);
})
Shuffle(rand);
for (i = 0; i < rand.length; i++) {
$('#fds img').eq(rand[i]).delay(i * 1000).fadeTo(2000, 1);
}
});
Demo: http://jsfiddle.net/rdc47yqL/
So, process is: get indexes of images, place them in array, randomize/shuffle array),iterate through randomized array,
and target images by using eq() selector.

Related

Add small scroll animation, when scrolled down then only need to show another row

I have some images as shown in the layout here.
Currently all images are showing at once. What I want instead is to show only single row of images at a time, I would like to have an animation, when user scolls, then show another row and hide previous one, similariy next scrolling show next row. In case if user scroll upwards, we need to show images from previous row. Like that.
Can't get a clue on how to achieve it. Here, is my current code.
.parallax-window {
min-height: 400px;
background: transparent;
}
.service-box-container{
display: grid;
grid-template-columns: repeat(6, 2fr);
gap: 15px;
}
.service-box-container .service-box{
grid-column: span 2;
}
.service-box-container .service-box:nth-child(5n+4), .service-box-container .service-box:nth-child(5n+5) {
grid-column: span 3;
}
.service-box-container img{
max-width: 100%;
width: 100%;
object-fit: cover;
object-position: center;
height: 460px;
border-radius: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/parallax.js/1.4.2/parallax.min.js"></script>
<div class="service-box-container parallax-window" data-parallax="scroll" data-image-src="https://www.fillmurray.com/640/360">
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
<div class="service-box">
<figure>
<img src="https://via.placeholder.com/350" class="" alt="">
</figure>
</div>
</div>
<script>
$(document).ready(function(){
$('.parallax-window').parallax({});
});
</script>
#PS: I have used parallax js as well, so I would love to keep those parallax effect as well.
Here is the jsfiddle.
Something like the following might be what you are looking for.
Note: You should run the following snippet in "Full-page" mode.
<!doctype html>
<html>
<head>
<title>Test</title>
<meta name=viewport content="width=device-width,initial-scale=1">
<meta charset="utf-8">
<style>
#scrollbox {
overflow-y: scroll;
overflow-x: auto;
background-color: #eee;
float: left;
}
.service-box {
margin: 10px;
text-align: center;
}
.service-box img {
margin: 0px;
}
</style>
</head>
<body>
<div id="scrollbox">
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img alt="Fourth of July 2022" height="200" src="https://google.com/logos/doodles/2022/fourth-of-july-2022-6753651837109457-law.gif" title="Fourth of July 2022" width="500">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
<div class="service-box">
<img src="https://www.fillmurray.com/640/360" class="" alt="">
</div>
</div>
<script>
// When we get to within 30 pixels of the bottom of a row,
// we show the next row:
const THRESHOLD = 30;
window.onload = function() {
let s = document.getElementById('scrollbox');
let children = s.children;
let scrollHeight;
let maxHeight = 0;
let maxWidth = 0;
for (let i = 0; i < children.length; ++i) {
let child = children[i];
let style = child.currentStyle || window.getComputedStyle(child);
if (i === 0) {
scrollHeight = child.offsetTop - parseInt(style.marginTop);
}
let heightWithoutMargins = parseInt(style.height) +
parseInt(style.paddingTop) +
parseInt(style.paddingBottom) +
parseInt(style.borderTop) +
parseInt(style.borderBottom);
let totalHeight = heightWithoutMargins + parseInt(style.marginTop) + parseInt(style.marginBottom);
if (totalHeight > maxHeight) {
maxHeight = totalHeight;
}
let widthWithoutMargins = parseInt(style.width) +
parseInt(style.paddingLeft) +
parseInt(style.paddingRight) +
parseInt(style.borderLeft) +
parseInt(style.borderRight);
let totalWidth = widthWithoutMargins + parseInt(style.marginLeft) + parseInt(style.marginRight);
if (totalWidth > maxWidth) {
maxWidth = totalWidth;
}
child.top = child.offsetTop - scrollHeight;
child.bottom = child.top + heightWithoutMargins - THRESHOLD;
child.marginTop = parseInt(style.marginTop);
child.style.visibility = 'hidden';
}
s.style.height = maxHeight + 'px';
// Allow for the scrollbar:
s.style.width = (maxWidth + 18) + 'px';
let currentVisible = null;
let currentY = 0;
s.onscroll = function() {
let yTop = s.scrollTop;
let yBottom = yTop + maxHeight;
// Find all visible:
let visible = [];
for (let i = 0; i < children.length; ++i) {
let child = children[i];
let top = child.top;
let bottom = child.bottom;
if ((top >= yTop && bottom < yBottom) || (top< yTop && bottom > yTop)) {
visible.push(children[i]);
}
}
if (currentVisible) {
currentVisible.style.visibility = 'hidden';
}
if (visible.length) {
currentVisible = yTop >= currentY ? visible.pop() : visible[0];
currentVisible.style.visibility = 'visible';
}
else {
currentVisible = null;
}
currentY = yTop;
};
s.onscroll(); // kick things off
};
</script>
</body>
</html>
window.addEventListener('scroll', function () {
console.log(scrollY); //You will see numbers increasing and decreasing in console
if(scrollY === yournecessarynumber) {
// For appearing the necessary height in the window.innerHeight
// If you replace with yournecessarynumber with number the function below will start working after reacing that number and animate your element with appearing from bottom and opacity
element.style.transform: 'scale(1), translateY(0)',
element.style.display: 'block'
}
})
// You should use it for recognizing the style and giving alter in animation
element.style.transform: 'scale(0), translateY(1000%)',
element.style.display: 'none'

Trying to make a simple slideshow with setinterval() in Js

No onClick, just automatic change of image and caption every second.
Here's the code I have so far:
HTML:
<div class="col-md-6">
<h2>Fishing Slide Show</h2>
<h4 id="caption">Casting on the Upper Kings</h4>
<img id="slide" src="images/casting1.jpg" alt="" height="200px">
<div id="slides">
<img src="images/casting1.jpg" alt="Casting on the Upper Kings" height="200px">
<img src="images/casting2.jpg" alt="Casting on the Lower Kings" height="200px">
<img src="images/catchrelease.jpg" alt="Catch and Release on the Big Horn" height="200px">
<img src="images/fish.jpg" alt="Catching on the South Fork" height="200px">
<img src="images/lures.jpg" alt="The Lures for Catching" height="200px">
</div>
</div>
And this is my Javascript:
var img_array = ["images/casting1.jpg", "images/casting2.jpg"
,"images/catchrelease.jpg", "images/fish.jpg"
,"images/lures.jpg"];
var alt_array = ["Casting on the Upper Kings", "Casting on the Lower Kings",
"Catch and Release on the Big Horn", "Catching on the South Fork",
"The Lures for Catching"];
var currImg = 0;
var currAlt = 0;
function slideShow();{
img.src = img_array[index++, img_array.length];
img.alt = alt_array[index++, alt_array.length];
//document.getElementById("slides").src = img_array[currImg];
//document.getElementById("slides").alt = alt_array[currAlt];
}
let stop = setInterval(slideShow, 1000);
Here's a simple slideshow I made.
You simply have the first slide have the active tab, then you use .nextElementSibling to get the next slide.
function runSlideshow() {
const current = document.querySelector(".slide.current");
current.classList.toggle("current");
current.nextElementSibling ?
current.nextElementSibling.classList.toggle("current") :
clearInterval(slideshow);
}
const slideshow = setInterval(runSlideshow, 2000);
.slide {
height: 200px;
display: none;
}
.slide.current {
display: block;
}
<div id="container">
<img class="slide current" src="https://cataas.com/cat/6136c137ebad510011b54b90" />
<img class="slide" src="https://cataas.com/cat/6136be59ebad510011b54b88" />
<img class="slide" src="https://cataas.com/cat/61307aa542fcca001bb841f5" />
<img class="slide" src="https://cataas.com/cat/6123cb0fe8503700118bdb62" />
<img class="slide" src="https://cataas.com/cat/6120c8103a342d0013920e23" />
</div>

Multiple Image Slideshow Javascript Issue

I have 4 image slideshows, and I am having trouble with the Javascript. I have given the div both an id and a class name. The class imageContainer is used in the CSS. Therefore I am using id for the Javascript, but it won't work. I can do this with one image slideshow, but I need to have multiple slideshows on the same page.
The HTML code below is not my actual code, it is just placeholder code I wrote to show what I want to achieve. The Javascript code, however, is my actual code.
I appreciate the help.
function openWin(url) {
aWindow = window.open(url, "Window 1", "width=400, height=400");
}
var numslides = 0,
currentslide = 0;
var slides = new Array();
function MakeSlideShow() {
container = document.getElementsById("Gallery");
for (i = 0; i < imgs.length; i++) {
if (imgs[i].className != "imga") continue;
slides[numslides] = imgs[i];
if (numslides == 0) {
imgs[i].style.display = "block";
} else {
imgs[i].style.display = "none";
}
imgs[i].onclick = NextSlide;
numslides++;
}
}
function NextSlide() {
slides[currentslide].style.display = "none";
currentslide++;
if (currentslide >= numslides) currentslide = 0;
slides[currentslide].style.display = "block";
}
window.onload = MakeSlideShow;
<h2>Gallery 1</h2>
<div id="Gallery" class="imageContainer">
<img class="img" src="https://via.placeholder.com/100" />
<img class="img" src="https://via.placeholder.com/101" />
<img class="img" src="https://via.placeholder.com/102" />
<img class="img" src="https://via.placeholder.com/103" />
</div>
<h2>Gallery 2</h2>
<div id="Gallery" class="imageContainer">
<img class="img" src="https://via.placeholder.com/106" />
<img class="img" src="https://via.placeholder.com/107" />
<img class="img" src="https://via.placeholder.com/108" />
<img class="img" src="https://via.placeholder.com/109" />
</div>
<h2>Gallery 3</h2>
<div id="Gallery" class="imageContainer">
<img class="img" src="https://via.placeholder.com/110" />
<img class="img" src="https://via.placeholder.com/111" />
<img class="img" src="https://via.placeholder.com/112" />
<img class="img" src="https://via.placeholder.com/113" />
</div>
<h2>Gallery 4</h2>
<div id="Gallery" class="imageContainer">
<img class="img" src="https://via.placeholder.com/114" />
<img class="img" src="https://via.placeholder.com/115" />
<img class="img" src="https://via.placeholder.com/116" />
<img class="img" src="https://via.placeholder.com/117" />
</div>
ID's must be unique, so, the best way to do this is get all galleries by their class name and start getting images from there. Note that slides is now a bidimensional array to hold gallery and images, something like:
slides[0] = [image100, image101, image102, image103];
I've added indexes for gallery and image to each picture using dataset propperty, so, you only need to know wich image was clicked instead of trying to get from currentslide. Also, numslides is provided from gallery length.
var slides = new Array();
function MakeSlideShow()
{
var containers = document.getElementsByClassName("imageContainer");
for(var x = 0; x < containers.length; x++) {
// Create gallery
slides[x] = new Array();
var imgs = containers[x].children;
for(var i = 0; i < imgs.length; i++) {
// Add image to current gallery
slides[x][i] = imgs[i];
// Set it's display, note that at least 1 images was added
if(slides[x].length == 1) {
imgs[i].style.display = 'block';
} else {
imgs[i].style.display = 'none';
}
// Add gallery and image info
imgs[i].dataset.gallery = x;
imgs[i].dataset.img = i;
// Add onclick event, but we need to send current image
imgs[i].onclick = function() {
nextSlide(this);
}
}
}
}
// This function needs to know wich img was clicked
function nextSlide(pic)
{
// Get gallery and image from dataset to know what to do
var gallery = pic.dataset.gallery;
var img = pic.dataset.img;
// Hide current image
slides[gallery][img].style.display = 'none';
// Search for next
img ++;
// No more images availabel, go to first
if(img == slides[gallery].length) {
img = 0;
}
// Show next image
slides[gallery][img].style.display = 'block';
}
window.onload = MakeSlideShow;
<h2>Gallery 1</h2>
<div id="Gallery1" class="imageContainer">
<img class="img" src="https://via.placeholder.com/100" />
<img class="img" src="https://via.placeholder.com/101" />
<img class="img" src="https://via.placeholder.com/102" />
<img class="img" src="https://via.placeholder.com/103" />
</div>
<h2>Gallery 2</h2>
<div id="Gallery2" class="imageContainer">
<img class="img" src="https://via.placeholder.com/106" />
<img class="img" src="https://via.placeholder.com/107" />
<img class="img" src="https://via.placeholder.com/108" />
<img class="img" src="https://via.placeholder.com/109" />
</div>
<h2>Gallery 3</h2>
<div id="Gallery3" class="imageContainer">
<img class="img" src="https://via.placeholder.com/110" />
<img class="img" src="https://via.placeholder.com/111" />
<img class="img" src="https://via.placeholder.com/112" />
<img class="img" src="https://via.placeholder.com/113" />
</div>
<h2>Gallery 4</h2>
<div id="Gallery4" class="imageContainer">
<img class="img" src="https://via.placeholder.com/114" />
<img class="img" src="https://via.placeholder.com/115" />
<img class="img" src="https://via.placeholder.com/116" />
<img class="img" src="https://via.placeholder.com/117" />
</div>

Counting classes then specify the amount to change

I have the following JavaScript function where I am counting the total divs with a classname, but not all must be changed, that is specified with the amount param. So there are 4 classes, so the each counts the classes and then the for loop changes them according to the amount param.
Yet this doesn't seem to work, see commented out version too -
function changeImages(change, amount) {
var $projImg = $('.proj-img');
$.each($projImg, function(i) {
$(this).attr('src', 'src/img/' + change + 'Slide' + (i + 1) + '.png');
});
// $.each($projImg, function (j) {
// for(var i = 0; i < amount; i++){
// $(this).attr('src', 'src/img/' + change + 'Slide' + (i) + '.png');
// }
// });
}
You could use an array and then slice it so that it contains no more than the amount number of elements, no jQuery required:
function changeImages(change, amount) {
const projImgs = [...document.querySelectorAll('.proj-img')]
.slice(0, amount);
projImgs.forEach((img, i) => {
img.src = 'src/img/' + change + 'Slide' + (i + 1) + '.png';
});
}
Use the :lt() selector
var amount = 5,change = '';
$('.proj-img:lt('+amount+')').map(function(i,el){
$(el).attr('src', 'src/img/' + change + 'Slide' + (i + 1) + '.png');
});
console.log($('.proj-img'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
<img src="" alt="" class="proj-img">
Just use jQuery's .slice():
function changeImages(amount) {
const $matches = $('.foo').slice(0, amount);
$matches.addClass('bar');
}
changeImages(10);
.foo {
border-bottom: 8px solid red;
}
.bar {
border-color: cyan;
}
.foo:hover {
border-color: black;
}
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<div class="foo"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Your JS code would then look like this:
function changeImages(change, amount) {
$('img').slice(0, amount).each((i, el) => {
el.setAttribute('src', `https://place-hold.it/200x200&text=${ change }-${ i + 1 }`);
});
}
changeImages('UPDATED', 3);
body {
margin: 0;
}
img {
float: left;
max-width: 20vw;
}
<img src="https://place-hold.it/200x200&text=DEFAULT" />
<img src="https://place-hold.it/200x200&text=DEFAULT" />
<img src="https://place-hold.it/200x200&text=DEFAULT" />
<img src="https://place-hold.it/200x200&text=DEFAULT" />
<img src="https://place-hold.it/200x200&text=DEFAULT" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Also, regarding :lt(), keep in mind that's not part of the CSS specification, as stated in the docs:
Because :lt() is a jQuery extension and not part of the CSS specification, queries using :lt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(0, index) instead.

How do I change image B's src to Image A's src when Image A is clicked with Javascript?

Goal: When image A is clicked that same image appears at the bottom of the page. Needs to work for more than one image.
When image A is clicked I need image B to now have the same src as image A.
Or to Generate a copy of the clicked image.
This should also be able to work for several pictures.
So for example if I clicked 5 images (Image A-01, A-02, A-03, A-04, A-05) on the screen....
At the bottom of the page there should be those 5 images
I've tried
HTML:
<img id="myImage" src="http://placehold.it/150">
<img id="new" src="http://placehold.it/200">
JS
$(function(){
$("#myImage").click(function() {
$("#new").attr("src","http://placehold.it/150")
});
});
Which works to replace one image but I need the same code to work for multiple image clicks.
Another proposal with an event listener.
var imgCount = 5,
i;
for (i = 1; i <= 5; i++) {
document.getElementById('img' + i).addEventListener('click', setImg('img' + i), false);
}
function setImg(id) {
return function () {
var img = document.createElement('img');
if (imgCount) {
imgCount--;
img.src = document.getElementById(id).src;
document.getElementById('footer').appendChild(img);
};
}
}
<img id="img1" src="http://lorempixel.com/200/100/city/1" />
<img id="img2" src="http://lorempixel.com/200/100/city/2" />
<img id="img3" src="http://lorempixel.com/200/100/city/3" />
<img id="img4" src="http://lorempixel.com/200/100/city/4" />
<img id="img5" src="http://lorempixel.com/200/100/city/5" />
<div id="footer"></div>
This will help you out
function changeSrc(id){
document.getElementById('footer').src = document.getElementById(id).src;
}
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" height="200" width="auto" id="img1" onclick="changeSrc(this.id)"/>
<img src="https://images-eu.ssl-images-amazon.com/images/G/02/uk-electronics/product_content/Nikon/ECS-NK1308141-B00ECGX8FM-2L-1024w683q85s10-BKEH_London_Paris__0316.jpg" height="200" width="auto" id="img2" class="image" onclick="changeSrc(this.id)"/>
<img src="" height="200" width="auto" id="footer" class="image" />
Try this:
<img src="https://randomuser.me/api/portraits/thumb/men/57.jpg" class="a">
<img src="https://randomuser.me/api/portraits/thumb/women/14.jpg" class="a">
<img src="https://randomuser.me/api/portraits/thumb/women/7.jpg" class="a">
<br>
<div id="sectionB">
<img src="http://placehold.it/48" class="b">
<img src="http://placehold.it/48" class="b">
<img src="http://placehold.it/48" class="b">
</div>
<br>
<button id="reset">Reset</button>
===
$(function() {
$('.a').click(function() {
var img = $(this).attr('src');
var index = $(this).index();
$('.b').eq(index).attr('src', img);
})
$('#reset').click(function(){
$('.b').attr('src', 'http://placehold.it/48');
})
})
DEMO: https://jsfiddle.net/j359yfor/

Categories

Resources