Image can't be displayed when image is onclick - javascript

Functionality:
When user navigates to the main menu page, a list of image will be displayed. When user click on a specific image within the container in the main menu, a second image will be displayed in a form of pop-up display image.
Lastly, user can click on the second image and the third image will be displayed.
All the images are grouped into different arrays, all images are linked; meaning, ImageArrayA[0] = ImageArrayB[0] = ImageArrayC[0].
What has been done:
I have 3 sets of array of image source:
1.) All images source Array 2.) Secondary Image Source Array(pop-up display) 3.) Third Image Source Array
I have grab all images from array(All images source), and insert into a particular location say: <div class="Container"> <div id= "list" class="innerScroll"></div></div> . The images are displayed in accordance to alphabetical order.
Secondly, I have done an onclick in regards to the and append the second array to the first array, hence, when user clicks on the primary image in the main menu, the corresponding second image will be displayed.
Lastly, I have tried to do the same method for dispplaying the third image as that of getting the second image.
var BrandNameArray = ['http://lorempizza.com/380/240',
'http://dummyimage.com/250/ffffff/000000',
'http://lorempixel.com/g/400/200/',
'http://lorempixel.com/g/400/200/sports/'
];
var SecondImage = ['http://lorempizza.com/380/240',
'http://dummyimage.com/250/ffffff/000000',
'http://lorempixel.com/g/400/200/',
'http://lorempixel.com/g/400/200/sports/'
];
var ThirdImage = ['http://lorempizza.com/380/240',
'http://dummyimage.com/250/ffffff/000000',
'http://lorempixel.com/g/400/200/',
'http://lorempixel.com/g/400/200/sports/'
];
var container = document.getElementById('list');
var docFrag = document.createDocumentFragment();
var x = 0;
BrandNameArray.forEach(function(url, index, originalArray) {
var img = document.createElement('img');
img.id = "Logo-" + x;
img.src = url;
docFrag.appendChild(img);
var selectedOffer = SecondImage[index];
//Choose Brand with popUp
img.onclick = function() {
$('#SecondImage').fadeIn({
duration: slideDuration,
queue: false
});
$("#Description").attr('src', selectedOffer).show();
};
selectedOffer.onclick = function() {
console.log("index" + index);
var PhotoFrame = ThirdImage[index];
console.log("PhotoFrame" + PhotoFrame);
$("#Chosen_ThirdImage").attr('src', PhotoFrame).show();
};
x++;
});
container.appendChild(docFrag);
.Container {
position: absolute;
top: 300px;
left: 300px;
height: 600px;
width: 1260px;
}
.innerScroll {
position: relative;
width: 1250px;
height: 600px;
font-size: 25px;
color: #8d8989 !important;
overflow-y: scroll;
}
<div id="FirstImage" align="center" style="position:absolute; width:1920px; height:1080px; background-repeat: no-repeat; z-index=3; top:0px; left:0px;">
<!--Container to display all alphabetically sorted images-->
<div class="Container">
<div id="list" class="innerScroll"></div>
</div>
</div>
<div id="SecondImage" class="menu" align="center" style="position:absolute; width:1920px; height:1080px; background-repeat: no-repeat; display:none; top:0px; left:0px; z-index=10;">
<img id="Description" style="position:absolute; top:124px; left:534px; z-index=99;">
</div>
<div id="ThirdImage" align="center" style="position:absolute; width:1080px; height:1920px; background-repeat: no-repeat; display: none; z-index=14; top:0px; left:0px; ">
<!--Photoframe that is selected from brand chosen-->
<img id="Chosen_ThirdImage" style=" position:absolute; width:1920px; height:1080px; top:0px; left:0px;" />
</div>
Issue:
I have managed to get the second image to display correctly when user clicks on the first image, however, when user clicks on the second image. The third image fails to load and display.
Meaning: when user clicks on imageA[0], i will get imageB[0] and also imageC[0] and if i were to click imageA[3], i will get imageB[3] and also imageC[3].
I would like to ask what have i done wrong. please help, thank you.

selectedOffer that you are trying to add an event handler to appears to just be a string, not an element in the DOM:
var selectedOffer = SecondImage[index];
from
var SecondImage = ['ht_p://lorempizza.com/380/240',
'ht_p://dummyimage.com/250/ffffff/000000',...
ie the string 'http://dummyimage.com/250/ffffff/000000'
The first image you assign on click to is an element you created with:
var img = document.createElement('img');
...
docFrag.appendChild(img);
You need #SecondImage to be clickable

Well not directly answering your question, but I can suggest how I would go about creating the above interface.
Lets say that the images are having the ids with the imgid as the prefix and then their serial no or whatever unique id is there.
Then I shall add the pop images absolute urls or just the name inside the data attributes namely data-first and data-second respectively.
For this example, I'll go witht the full absolute URLs.
<div class="containerofimg">
<img src="xyz/img/xyz.jpg" id="imgid234" data-first="http://lorempixel.com/600/300/sports/FirstImageA" data-second="http://lorempixel.com/600/300/sports/SecondImageA" />
<img src="xyz/img/abc.jpg" id="imgid235" data-first="http://lorempixel.com/600/300/sports/FirstImageB" data-second="http://lorempixel.com/600/300/sports/SecondImageB"/>
<img src="xyz/img/pqr.jpg" id="imgid236" data-first="http://lorempixel.com/600/300/sports/FirstImageC" data-second="http://lorempixel.com/600/300/sports/SecondImageC"/>
</div>
Then I shall use the jQuery .data() or .attr() to grab the first and second overlay Images, and display them inside the modal.
As shown in this Demo.

Related

CSS: Using transform to move an element to a specific place on the DOM

I have an image (svg) that start out in the middle of the page on load. As the user scrolls down, I want to have that image drift up and to the left so that it eventually gets pinned in the top left corner.
I tried using translate with the scroll number but I feel like it's a guessing game on where the image will end up and what path it takes. Here is what I had so far.
window.addEventListener('scroll', function(evt) {
let image = document.getElementById('image');
let scrollY = window.pageYOffset;
// how do I use the scrollY to accomplish this?
image.style.transform = ??
<div style="text-align: center; background: blue; height: 900px;">
<img id="image" style=" margin-top: 200px; " src="mySvg.svg" />
</div>
for working document.getElementById('image'); need to have is corresponded element.
<img id="image" style=" margin-top: 200px; " src="mySvg.svg" />

Restricting image inside div and arrange those div inside a div

I am using Gridster for webpage.The widgets have images on it.These images can be added and deleted with + and X button.
On clicking +, a modal opens and displays images which I retrieve from server dynamically
The modal is the <div1 class="modal-body">.In that I want <div2 class="outerdiv">.
In a single row in div1 there should be an equal number of div2 is what I want.Entire div1 should have div2 which are separated properly from each other and same in number on each row.
Then comes <div3 class="innerdiv"> which will be inside each div2.Thediv3` will contain my image which should fit properly within that div only.
Overall, when I open my modal the images should symmetrically and properly placed from each other
The CSS which I tried
.modal-body {
width: 100%;
position: relative;
text-align:center;
}
.outerdiv {
height: 90px;
width: 90px;
display:inline-block;
margin: 10px;
}
.innerdiv{
max-height:95%;
max-width:95%;
}
My actual code for server
<div class="modal-body">
{% for file in brands %}
{% load static %}
<div class="outerdiv"><div class="innerdiv"><img src="{% get_static_prefix %}images/brands/{{file}}"></div></div>
{% endfor %}
</div>
My representational image:
<div class="outerdiv"><div class="innerdiv"><img src="https://cdnd.icons8.com/wp-content/uploads/2015/07/Run-Command-100.png"></div></div>
I dont know if what I did is right or not because when I when I do inspect element in browser I see that image is coming out of the div also sometimes the images almost overlap each other
Jquery function which applies preset class to clicked image.I want it to be applied to innverdiv
var parentLI;
$(document).on("click", ".addmorebrands", function() {
parentLI = $(this).closest('li');
$('#exampleModalCenter').modal('show');
$('#exampleModalCenter img').click(function(){
$(this).addClass('preselect');
$(this).siblings().removeClass('preselect');
selectedImageSRC = $(this).attr('src');
})
});
Fiddle
Move the preselect class to the innerdiv.
Make some of these css modifications:
img {
width: 100%;
height: 100%;
}
.preselect{
background: lightgreen;
border: 1px solid black;
}
checkout fiddle

JS Get background image then display in lightbox

I have a set of divs with background images. Im trying to get the image onclick then display it in a lightbox. When the lightbox pops, the image i get in undefined. I can get the image url to display correctly through console. I know lbImg.src = galImg.src; is incorrect but i am unsure how to fix it.
HTML
<div id="lightbox">
<span class="closeBTN">×</span>
<img class="lbContent" src="">
</div>
<div class="imgCon">
</div>
CSS
.imgCon
{
height: 500px;
width: 15%;
min-width: 200px;
background-image: url(../images/01.jpg);
background-position: center;
background-size: auto 500px;
margin-bottom: 66px;
}
JS
var lightbox = document.querySelector('#lightbox')
img = document.querySelector('.imgCon')
span = document.querySelector('.closeBTN')
lbImg = document.querySelector('.lbContent')
img.onclick = (function(){
lightbox.style.display = "block";
var galImg = window.getComputedStyle(this,
null).getPropertyValue("background-Image");
lbImg.src = galImg.src;
//console.log(galImg);
})
span.onclick = (function(){
lightbox.style.display = "none";
});
You get the background-image itself in galImg, so you need to set it directly (it is a string and does not have a .src property).
But, the value of galImg we obtain from getComputedStyle is a string like as: "url("file:.../Desktop/1.png")". We have to remove the url( part and also the quotation marks ("...") from it:
lbImg.src = galImg.slice(4, -1).replace(/"/g, "");
Can you please try lbImg.setAttribute('src', galImg.src).

Add image over text using jquery

I'm trying to add an image over some text that I have. This is similar to retailmenot.com's reveal coupon code. When a user clicks on the image the image is removed and reveals the text underneath while simultaneously linking the user to an external url.
The base layer can be as follows:
<div class="base">
<h3>Some text</h3>
</div>
I want to load an image with the following over it when the text is clicked:
<div class="overlay">
<img src="http://example.com/image.jpg"/>
</div>
The height of the base layer with class "base" is variable, so the image has to be resized to fit it. I have a working example where I place the image and then resize it, but this creates issues when javascript may not be enabled as the image fails to be resized and looks messy. I want the script to fall back to just showing the underlying text if javascript is disabled.
How can I add and automatically resize such an overlay on page load using jquery or javascript?
You can do it like this:
$(document).ready(function () {
//Set overlay position and dimension to same as base
$base = $(".base");
$overlay = $(".overlay");
$overlay.offset($base.offset());
$overlay.height($base.outerHeight());
$overlay.width($base.outerWidth());
$overlay.show();
//Hide overlay on click (show hidden text)
$(".overlay").click(function () {
$(this).fadeOut();
});
});
and with css:
.overlay{
/* Hide overlay if no js */
position: absolute;
display: none;
}
Check it out here: JSFiddle
If you can have the overlay in the base, as such:
<div class="base">
<h3>Some text</h3>
<div class="overlay">
<img src="http://example.com/image.jpg"/>
</div>
</div>
You can css this, no need for javascript:
.base{
position: relateive;
}
.overlay{
position: absolute; /* or fixed if scrollbars involved */
display: none;
left: 0;
right: 0;
top: 0;
bottom: 0;
/* or replace right and bottom with: */
/* width: 100%;
height: 100%; */
}
You can now use javascript to toggle visibility:
$('.overlay').fadeIn();
Let your html page has this following code
<div class="base">
</div>
Don't place any code about your image in html page. And then in your jQuery code.
var img = '<img src="http://example.com/image.jpg"/>';
var txt = 'Some text';
$(document).ready(function(){
$(this).find('.base').html(txt).show();
$(this).find('.base').click(function(){
if($(this).html() == img)
$(this).html(txt).show();
else
$(this).html(img).show();
});
});
This will solve your issue.

Can each new click produce a new image?

I know how to make an image appear using onclick or onMouseOver but how can I make each click produce the appropriate image not just in the same place but, for example, in a row, next to it's previous apperance? My idea is this: I click on reference1 and a picture1 shows up. Then I click on reference2 and a picture2 shows up next to the picture1 already displayed. Now I click on reference1 again, and I want to see pictures 1,2,1 in a row. How can I do that? My ideal would be to see the row rolling when filled, and a delete button deleting the last image in that row, even making the pictures jump out being called from the text field, but I can search for these myself. My greatest concern for now is new click=new image. Thank you.
Assuming this is relatively simplistic- you could keep track of the current position in a list of images, afterwards create a function that deals with the current image then increments this position. Have the onClick event call this function, and there you are.
An example of this in action, using JQuery, can be viewed here:
http://jsfiddle.net/8Q4LQ/
Here's an example.
<html>
<head>
<style>
.refimg { width: 100px; height: 100px; }
.choices a { margin-right: 2ex; }
.choices img { display: none; }
#target { display:block; width: 500px; overflow-x: scroll; border: 1px solid black; }
</style>
</head>
<body>
Choices:
<div class="choices">
ref1
ref2
<image id="image1" src="image1.gif" class="refimg" />
<image id="image2" src="image2.gif" class="refimg" />
</div>
<br />
Selections: <input type="button" value="Delete" onclick="delImage()" />
<nobr id="target">
</nobr>
<script>
function putImage(src) {
var a = src.cloneNode(true);
a.id = ''; //clear id to prevent duplicate id
target.appendChild(a);
a.scrollIntoView(true);
return false;
}
function delImage() {
var a=target.children;
if (a.length>0) target.removeChild(a[a.length-1]);
}
target.style.height=((target.offsetHeight-target.clientHeight)+100)+'px'; //extend height for scroll bar
</script>
</body>
</html>

Categories

Resources