cardShop is a div with children -> (images with different ID's and values)
every card value looks like this 2000,500 or this 1500,200
I want to sort the images by value in ascending order by the first number in the array.
The function is called by the blue button in the right corner.
Here is what I have done so far.
HTML DOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="script.js"></script>
<style>
#toSort{
background-color: #3b5998;
width:50px;
height:50px;
float:right;
}</style>
</head>
<body>
<div id = "cardShop" style="overflow:scroll">
<div id ="toSort" onclick="sorting()"></div>
<img id="card" height="400" width="250" src="./cardImages/blueEyes.jpg" value = 3000,2500>
<img id="card2" height="400" width="250" src="./cardImages/blue.jpg" value = 1400,1200>
<img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value = 1200,700>
<img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value = 3200,2500>
<img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value = 2500,2100>
<img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value = 2500,2100>
<img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value = 2000,1700>
<img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value = 2300,2100>
<img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value = 2400,2000>
<img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value = 1800,1600>
<img id="card11" height="400" width="250" src="./cardImages/curse.jpg" value = 2800,2200>
<img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value = 4500,3800>
</div>
</body>
</html>
JavaScript file
function sorting() {
let deck = [].slice.call(document.getElementById("cardShop").children)
for (let i = 2; i < deck.length; i++) {
let elementValue = deck[i].getAttribute('value').split(",").map(Number);
}
}
const sorting = () => {
const images = document.getElementsByTagName('img');
const deck = [].slice.call(images);
const container = document.getElementById('cardShop');
const deckSorted = deck.sort((a, b) => {
const aToSort = a.getAttribute('value').split(',')[0];
const bToSort = b.getAttribute('value').split(',')[0];
return aToSort - bToSort;
});
for(let i = 0; i < deck.length; i++) {
deck[i].parentNode.removeChild(deck[i]);
container.append(deckSorted[i]);
}
}
<div id="cardShop" style="overflow:scroll">
<div id ="toSort" onclick="sorting()">Sort</div>
<img id="card" height="400" width="250" src="./cardImages/blueEyes.jpg" value = 3000,2500>
<img id="card2" height="400" width="250" src="./cardImages/blue.jpg" value = 1400,1200>
<img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value = 1200,700>
<img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value = 3200,2500>
<img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value = 2500,2100>
<img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value = 2500,2100>
<img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value = 2000,1700>
<img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value = 2300,2100>
<img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value = 2400,2000>
<img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value = 1800,1600>
<img id="card11" height="400" width="250" src="./cardImages/curse.jpg" value = 2800,2200>
<img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value = 4500,3800>
</div>
You can do it using sort and after that just remove the old element and append the new element, like this.
You are on the right track,I finished the code for you.
The code pushes all the images to an array with json elements that contains the element and the value (the left one). It then sorts them in the right order, removes all the images and inserts them again in the correct order.
Working example:
https://playcode.io/537993
function sorting() {
let deck = [].slice.call(document.getElementById("cardShop").children)
let images = []
let button = document.getElementById('toSort');
for (let i = 1; i < deck.length; i++) {
let elementValue = deck[i].getAttribute('value').split(",").map(Number);
// add all values to a array with json info about the element
images.push({
'element': deck[i],
'value': elementValue[0]
});
}
// sort them on the value
images.sort((a, b) => {
return a.value - b.value;
})
// remove all elements in the div
document.getElementById('cardShop').innerHTML = '';
// add the button again
document.getElementById('cardShop').appendChild(button);
// Add the images again
images.forEach(image => {
document.getElementById('cardShop').appendChild(image.element)
})
}
const setup = () => {
const toSort = document.querySelector('#toSort');
toSort.addEventListener('click', sorting);
};
const sorting = () => {
let cardShop = document.querySelector('#cardShop');
let dek = [...cardShop.querySelectorAll('img')]
.sort((a,b) => {
let valuesA = a.getAttribute('value').split(',');
let valuesB = b.getAttribute('value').split(',');
let comparer = valuesA[0].localeCompare(valuesB[0]);
if(comparer === 0)
comparer = valuesA[1].localeCompare(valuesB[1]);
return comparer;
})
.map(node=>cardShop.appendChild(node));
};
//load
window.addEventListener('load', setup);
#toSort{
background-color: #3b5998;
width:50px;
height:50px;
float:right;
}
<!DOCTYPE html>
<html lang="en">
<body>
<div id = "cardShop" style="overflow:scroll">
<div id ="toSort"></div>
<img id="card" height="400" width="250" src="./cardImages/blueEyes.jpg" value="3000,2500">
<img id="card2" height="400" width="250" src="./cardImages/blue.jpg" value="1400,1200">
<img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value="1200,700">
<img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value="3200,2500">
<img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value="2500,2100">
<img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value="2500,2100">
<img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value="2000,1700">
<img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value="2300,2100">
<img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value="2400,2000">
<img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value="1800,1600">
<img id="card11" height="400" width="250" src="./cardImages/curse.jpg" value="2800,2200">
<img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value="4500,3800">
</div>
</body>
</html>
Your HTML markup was broken. Make sure you refactor it to <img ... value="1200,2500" /> instead of <img ... value=1 200,2500 />, because no Javascript will be able to handle it.
function sorting () {
const imgWrapper = document.getElementById('cardShop')
const deck = [].slice.call(imgWrapper.getElementsByTagName('img'))
deck.sort((currentImage, nextImage) => {
const currentImageValue = parseInt(currentImage.getAttribute('value').split(',')[0])
const nextImageValue = parseInt(nextImage.getAttribute('value').split(',')[0])
return currentImageValue - nextImageValue
})
// at this point the array has been sorted, but not the actual HTML!
console.log(deck)
// optional - if you want to redraw them with the new order in the HTML
const oldImgHTMLCollection = [].slice.call(imgWrapper.getElementsByTagName('img'))
oldImgHTMLCollection.forEach(oldImg => imgWrapper.removeChild(oldImg))
deck.forEach(newImg => imgWrapper.appendChild(newImg))
}
sorting()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="script.js"></script>
<style>
</style>
</head>
<body>
<div id="cardShop" style="overflow:scroll">
<div id="toClose"></div>
<div id="toSort"></div>
<img id="card" height="400" width="250" src="./cardImages/blueEyes.jpg" value="3000,2500">
<img id="card2" height="400" width="250" src="./cardImages/blue.jpg" value="1400,1200">
<img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value="1200,700">
<img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value="3200,2500">
<img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value="2500,2100">
<img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value="2500,2100">
<img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value="2000,1700">
<img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value="2300,2100">
<img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value="2400,2000">
<img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value="1800,1600">
<img id="card11" height="400" width="250" src="./cardImages/curse.jpg" value="2800,2200">
<img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value="4500,3800">
</div>
</body>
</html>
I want to make evaluation page that allow student to evaluate course:
Here jquery code that are used to change the color of the white images to yellow ones
<script type="text/javascript">
var clickedFlag = false;
$(".ratingStar").click(function () {
$(this).attr("src", "/Content/images/yellowstar.gif").prevAll("img.ratingStar").attr("src", "/Content/images/yellowstar.gif").attr("value",1);
});
$(".ratingStar, #radingDiv").mouseout(function () {
$(this).attr("src", "/Content/images/whitestar.gif");
});
$("#ratingDiv").mouseout(function () {
if (!clickedFlag) {
$(".ratingStar").attr("src", "/Content/images/whitestar.gif");
}
});
$(".ratingStar").click(function () {
clickedFlag = true;
$(".ratingStar").unbind("mouseout mouseover").css("cursor", "default");
var url = "/EvaluationQuestion/SendRating?r=" + $(this).attr("data-value") + "&s=5&id=#Model&url=#Url";
});
this is html code the display the images of stars
<div id="ratingDiv" class="smallText">
Poor
<img src="~/Content/images/whitestar.gif" alt="" class="ratingStar" data-value="1" />
<img src="~/Content/images/whitestar.gif" alt="" class="ratingStar" data-value="2" />
<img src="~/Content/images/whitestar.gif" alt="" class="ratingStar" data-value="3" />
<img src="~/Content/images/whitestar.gif" alt="" class="ratingStar" data-value="4" />
<img src="~/Content/images/whitestar.gif" alt="" class="ratingStar" data-value="5" />
Excellent
</div>
I want to receive the data value of these images in the action of this view
I've been trying since yesterday to do something a bit tricky (I guess).
I'm trying to do exactly what happens here: https://tympanus.net/Development/AnimatedResponsiveImageGrid/index2.html but this script is 8 years old and can't work with newer jQuery, anyway I wanted to redo it by myself.
What I need (well, what the marketing want me to do):
Having a 6 images grid which randomly crossfade to another image, but one by one. They should never repeat themselves.
So far I've done this, but all the crossfade are made 6 by 6. I want to do it, one by one, in a random order.
HTML
<div class="img-bank">
<img style="display:none" src="https://picsum.photos/210?image=0" />
<img style="display:none" src="https://picsum.photos/210?image=11" />
<img style="display:none" src="https://picsum.photos/210?image=21" />
<img style="display:none" src="https://picsum.photos/210?image=31" />
<img style="display:none" src="https://picsum.photos/210?image=41" />
<img style="display:none" src="https://picsum.photos/210?image=51" />
<img style="display:none" src="https://picsum.photos/210?image=61" />
<img style="display:none" src="https://picsum.photos/210?image=71" />
<img style="display:none" src="https://picsum.photos/210?image=81" />
<img style="display:none" src="https://picsum.photos/210?image=91" />
<img style="display:none" src="https://picsum.photos/210?image=101" />
<img style="display:none" src="https://picsum.photos/210?image=111" />
<img style="display:none" src="https://picsum.photos/210?image=121" />
<img style="display:none" src="https://picsum.photos/210?image=131" />
<img style="display:none" src="https://picsum.photos/210?image=141" />
<img style="display:none" src="https://picsum.photos/210?image=151" />
<img style="display:none" src="https://picsum.photos/210?image=161" />
<img style="display:none" src="https://picsum.photos/210?image=171" />
<img style="display:none" src="https://picsum.photos/210?image=181" />
<img style="display:none" src="https://picsum.photos/210?image=191" />
</div>
<div class="container galery">
<div class="row">
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=1" />
<img style="display:none;" class="img-fluid" src="" />
</div>
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=2" />
<img style="display:none;" class="img-fluid" src="" />
</div>
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=3" />
<img style="display:none;" class="img-fluid" src="" />
</div>
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=4" />
<img style="display:none;" class="img-fluid" src="" />
</div>
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=5" />
<img style="display:none;" class="img-fluid" src="" />
</div>
<div class="col-4">
<img class="img-fluid" src="https://placekitten.com/210/210?image=6" />
<img style="display:none;" class="img-fluid" src="" />
</div>
</div>
</div>
JS
$( document ).ready(function() {
var ids = [];
function initArray() {
$(".img-bank img").each(function() {
ids.push($(this).attr("src"));
})
}
function randomArray() {
// copie du tableau d'ids car il va etre modifié
var tempIds = ids.slice();
// init du tableau de resultat
var myIds = [];
for (var i = 0; i < 6; i++) {
// Recupere un int random
var randomId = (Math.floor(Math.random() * tempIds.length) + 1);
// Recupere la valeur random
var myId = tempIds[randomId - 1];
// Ajout de la valeur random au tableau de resultat
myIds.push(myId);
// Recupere l'index de la valeur random pour la suppression
var pos = tempIds.indexOf(myId);
// Suppression de la valeur choisie pour eviter de retomber dessus
tempIds.splice(pos, 1);
}
return myIds;
}
initArray();
function changeSrc() {
var result = randomArray();
$(".galery img:hidden").each(function(index) {
$(this).attr("src", result[index]);
});
$(".galery img").fadeToggle(1500);
}
setInterval(function() {
changeSrc();
}, 2000);
});
LESS
.galery {
margin-top: 30px;
.row > div {
position:relative;
height: 240px;
width: 240px;
img {
position: absolute;
top: 0;
left: 15;
}
}
}
https://jsfiddle.net/N_3G/ju0comn2/
Can someone help me with this please?
You can select a cell col-4 at random, then apply the logic to only the 2x img in that cell.
Without changing too much of your existing code, change to:
var cells = $(".galery .col-4");
var randomId = (Math.floor(Math.random() * cells.length));
var cell = cells.eq(randomId);
cell.find("img:hidden").each(function(index) {
$(this).attr("src", result[index]);
});
cell.find("img").fadeToggle(1500);
Updated fiddle: https://jsfiddle.net/ju0comn2/1/
Due to the nature of Math.random() you will notice it runs the same images in the same order - seed the random. You'll also get the same image replacing with the same image.
For a rudimentary fix for duplicated images, check if any of the visible images have the same src as the new src:
var result = randomArray();
var cells = $(".galery .col-4");
var randomId = (Math.floor(Math.random() * cells.length));
var newsrc = result[0];
if ($(".galery img[src='" + newsrc + "']:visible").length > 0) {
changeSrc();
}
else {
var cell = cells.eq(randomId);
cell.find("img:hidden").each(function(index) {
$(this).attr("src", result[index]);
});
cell.find("img").fadeToggle(1500);
}
This could be handled with a while loop to keep getting randomIds, but as long as you have more images than panels, the recursive call is unlikely to stack overflow.
Updated fiddle: https://jsfiddle.net/ju0comn2/2/
I've created a webpage with many (over 100) paired flash cards with Question (image) flipping to Answer (image) when clicked. If possible I would like to be able to randomise the order in which the question/answer pairs load with each page load/refresh.
<div class="content4Column gap">
<div class="card-container">
<div class="card click" data-direction="left">
<div class="front">
<img src="Intermolecular/Q1.png" width="100%" height="100%" alt="">
</div>
<div class="back">
<img src="Intermolecular/A1.png" width="100%" height="100%" alt="">
</div></div></div></div>
Try
var QAPairs = [];
var QATotal = 200; // total numbers of QA pairs
var QA = 20; //number of QA pairs you want. Set to QATotal to include all QA pairs
while(QAPairs.length != QA){
var rand = Math.floor(Math.random()*QATotal);
if(QAPairs.indexOf(rand) == -1){
QAPairs.push(rand);
// If you want to show them all at a time
document.getElementsByClassName("card click")[0].innerHTML = document.getElementsByClassName("card click")[0].innerHTML + '<div class="front">\
<img src="Intermolecular/Q' + QAPairs[i] + '.png" width="100%" height="100%" alt="" />\
</div>\
<div class="back">\
<img src="Intermolecular/A' + QAPairs[i] + '.png" width="100%" height="100%" alt="" />\
</div>';
}
}
// if you want to show them one at a time
var i = 0;
function updateCard(){
document.getElementsByClassName("card click")[0].innerHTML = '<div class="front">\
<img src="Intermolecular/Q' + QAPairs[i] + '.png" width="100%" height="100%" alt="" />\
</div>\
<div class="back">\
<img src="Intermolecular/A' + QAPairs[i] + '.png" width="100%" height="100%" alt="" />\
</div>';
i++;
}
I have this HTML:
<div class="page-loading-wrapper">
<div style="width: 168px" class="slide-content">
<div class="loading-panel" style="background-color: #dddddd; height: 120px; position: relative">
<div class="loading-text"><span class="loading-number">0</span>%</div>
<div class="loading-load">LOADING</div>
</div>
<img src="images/shkeer-loading.png" width="168" height="91" alt="" />
</div>
<div class="loading-bottom-line"></div>
</div>
<div id="maximage">
<div>
<img src="images/bg/bg.jpg" width="1580" height="889" alt="" />
</div>
<div>
<img src="images/bg/bg2.jpg" width="1352" height="748" alt="" />
</div>
<div>
<img src="images/bg/bg3.jpg" width="1285" height="803" alt="" />
</div>
<div>
<img src="images/bg/bg4.jpg" width="1290" height="807" alt="" />
</div>
<div>
<img src="images/bg/bg5.jpg" width="1295" height="921" alt="" />
</div>
<div>
<img src="images/bg/bg6.jpg" width="1280" height="750" alt="" />
</div>
<div>
<img src="images/bg/bg7.jpg" width="1280" height="750" alt="" />
</div>
</div>
and this Javascript code:
$('#maximage').maximage({
onImagesLoaded: function() {
$(".page-loading-wrapper").slideUp("slow");
},
cycleOptions: {
fx:'scrollHorz',
autostopCount: 1,
autostop: 1
}
});
and this code for loading:
function changeLoading() {
var height = parseInt($(".loading-panel").height()) + 5;
$(".loading-panel").css("height", height + "px");
if ( height == 325) {
clearInterval(t);
var t2 = setInterval(changeNum, 110);
}
}
function changeNum() {
var n = parseInt($(".loading-number").html()) + 1;
$(".loading-number").html(n);
if ( n == 100) {
clearInterval(t2);
}
}
var t = setInterval(changeLoading, 10);
I'm using MaxCycle (Fullscreen Slideshow for use with jQuery Cycle Plugin)
and I'm trying to display loading bar while loading all images because they're very big about 150kb for each image.
Demo
My loading bar is not fully functional as you can see. it just count until MaxCycle is loaded
onImagesLoaded: function() {
$(".page-loading-wrapper").slideUp("slow");
}
So how I can make this loading works 100% specially the percent number, to check each image is loaded or not?
Thank you very much.