Overlay text from a variable at the bottom of every image - javascript

I'm hoping to overlay text from a variable at the bottom of every image using jquery preferably, or javascript.
Here's the project I'm working on - http://codepen.io/chrismcintosh/pen/ejzut.
This is the first javascript I've ever produced so don't be too hard on me.
Can anyone point me in the right direction?

The question is a little light on the details but hopefully this helps. First, you need to add an element that will hold the data to your HTML. You can do this via JS instead of via HTML if you want, but here goes:
<img src="http://greatworkperks.com/sites/default/files/perks/RunorDye2.jpg"
class="tile" id="Run Or Dye" data-toggle="modal" data-target="#myModal"
onclick="myFunction(this.id)">
<!-- Below is the new div -->
<div id="RunOrDye_Overlay" class="overlay">Run or Dye</div>
Next, add the corresponding CSS style. Here is one that might work:
DIV.overlay{
position: relative;
display: inline;
top: 110px;
left: -290px;
background-color: #333333;
color: white;
}
At this point your overlay exists. I'm not sure if you need your JS to actually do anything at this point -- you could set up your JS to toggle this div when the user hovers over the image or something like that.
Does this help?

Instead of hard-coding the overlays into your html, you can use jQuery's .wrap() function to wrap each tile, and then append a text element to each wrapper.
// wrap each tile in a span
$('.tile').wrap('<span class="tile-wrapper"></span>')
// append the text to each wrapper
$('.tile-wrapper').each(function(){
var $this = $(this);
$this.append('<div class="image-text">' + $this.find('img').attr('id') + '</div>');
});
If you make each wrapper element position: relative and each text overlay position: absolute the overlays will automatically be positioned in the middle of each tile. Then you can adjust the overlay's bottom property to put them at the bottom of the tile.
Here's the updated CodePen

Related

How can I append an element to a specific area of the page using pixels

I am trying to append an image to a specific area of my page (the middle). At the moment the image is appearing at the very bottom of my page. Could anyone advise me how to change the location of where the image appears?
function picture(){
var img = new Image();
img.src = 'youwin.png';
document.body.appendChild(img);
}
} picture();
This is probably a great use case for position: absolute in your CSS. I've whipped up a little example, is this what you're looking to do?.
In the example that I linked, I wrapped a DIV around the image to make putting the image in a the centre a bit easier, but you can do this by styling the image on its own as well.
image {
position: absolute;
left: 0;
top: 0;
padding-top: 5px;
}
Play with the left and top values to move your image around!
If you want the image to be centred, I think the easiest thing to do is wrap a DIV around the image and style the div instead. Try text-align: center like in my example.
When you're looking at how to style the DIV, you can even take a look at flexbox for some options!

JQuery, resize div height to new content after previously setting height

I have a DIV element that initially has a height of 0 and opacity of o who's overflow is hidden, and it has some content in it.
<div style='height: 0px; opacity: 0px; display: none; overflow: hidden; border: 1px solid #000;' id='mydiv'>
Some Content<br><br>
Lots or more content
<br><br><br><br>
hello world and stuff
</div>
I am fading this DIV into view when you click a button, the code that fades it into view is inside a function and is the following:
function fadediv(){
mycontentheight=$('$mydiv')[0].scrollHeight;
$('$mydiv').
css('display','block').
animate({opaicty:1,height:mycontentheight},100);
}
But then I want to add or remove some content from the DIV, for example:
document.getElementById('mydiv').innerHTML += 'Some more content...';
When I add the new content or take some content away, the height of the div doesn't change, and I would expect this. It's been set with the jquery in the function above and I have the overflow set to hidden, so more content would be in the overflow of the element, this makes sense.
But I want to change the height of the div to be the height of the new content, and I can't figure out how to do this. I've tried the following and it doesn't work:
function addtodiv(){
document.getElementById('mydiv').innerHTML += 'Some more content...';
mycontentheight=$('$mydiv')[0].scrollHeight;
$('$mydiv').
animate({opaicty:1,height:mycontentheight},100);
}
the above function adds the new content to the div, fine, but it doesn't update the height of the div to the height of the new content.
am I doing something wrong? am i missing something?
thank you for your help!
You've got a few spelling mistakes, such as opaicty instead of opacity. You can find those and correct them yourself, since they are not relevant to the question. The main mistake you are making is that you are never updating the div size, which I did in updateSizing. You also need to select the specific div from the jQuery selector using [0] when using innerHTML, since it is not a jQuery method. Check out these changes on JSFiddle.

How to align the overlay with the list of icons on hover- JS /CSS

I have a list of <li>'s and a icon next to it which on hover shows an overlay with the information about the 'test'. something like below:
test1
test2
test3
and so on....
html:
<span class="account-info-icon"></span> // icon is the build using image sprites
<div id ="hover-container>
//details about the 'test1','test2'..so on
</div>
js:
$('span.account-info-icon').on("mouseenter", function(event){
$("#hover-container").show();
}).on("mouseout", function(){
$("#hover-container").hide();
});
The above code works fine to show/hide the div container on hover. However I'm having issues with the positioning of the overlay. im using css to position the overlay, as a result of which, the overlay is always positioned below irrespective of which ever icon i hover.
in short because im hard coding the values of the <div> conatiner the overlay always shows at one place and does not move as per the hover over the icons.
Below is the css im using to position the overlay.
CSS:
#hover-container{
display: none;
position: relative;
top: -750px;
left: 943px;
padding: 2px 0 0 9px;
}
Basically what i m trying is to allign the overlay per the flow of the hover. so when i hover over , say: 'test1' icon, the overlay should display next to it. I'm not sure if this is achievable via CSS or Js.
Any ideas appreciated!!!!
Thanks in advance!
To simplify this exercise, become familiar with two css position values: "position:relative" and "position:absolute". Also, proper container arrangement will help you get favorable results.
On the premise that #hover-container just happens to generically refer to a non-replicated ID property in your html, it can have this css definition:
#hover-container{
display:none;
position:absolute;
padding: 2px 0px 0px 9px;
left:100px;
}
Each instance of your span should then be in a wrapper container to help guide the hover to appear exactly where you want it:
.info-row-wrapper {
position:relative;
}
Pulling all of these together, you have:
<div class="info-row-wrapper">
<span class="account-info-icon"></span> // icon is the build using image sprites
<div id ="hover-container>
//details about the 'test1','test2'..so on
</div>
</div>
Here, the wrapper container gives a shell that the absolute positioned element appears inside of. The absolute positioned element respects the position of the parent html container that is explicitly positioned relative (if not already assigned a css position attribute)
please refer to the fiddle - http://jsfiddle.net/L33jo3j7/4/
Pretty much $el.hover() solves the thing.
and let me know if you have any doubts.
This looks better-
http://jsfiddle.net/L33jo3j7/4/

Interactive HTML webpage

EDIT: Thanks for a lot of great examples on how to solve these. I cant decide between who to accept yet, but I will go though all examples and see which I like the most. Great feedback guys! =D
I normally do these kind of things in flash, but this time it has to be compatible with mac, iPads and all those units too.
So, what do I need help with?
I've got a picture, with some "hotspots" on. I want to be able to click any of those hotspots to show some information.
This should be fairly basic and easy to achieve, but since I've never done this in html before I have to ask you guys =)
So, what would be the best way to do this? It have to be compatible with any browser and device, and it doesnt need to be very advanced. If it's possible to add effects to the box (sliding out, fading in, or anything like that) then thats a nice bonus, but not something I need.
Any help would be great!
BREAKDOWN:
I have a background image with some "hotspots" (numbers 1 and 2 in my example). The users should be able to either hover the mouse over any of these or click it to get more information, as seen in picture #2
This is that happens when you hover/click any of these hotspots.
Text and image is displayed inside a nice little info box.
If the user clicks "more information" it will open up even further to display more information if available. Like in this img:
I don't think the Javascript approach is really necessary here. I created a little CSS-only mock-up for you on JSBin.
Basically the point is that you enclose the image in a relatively positioned div, then absolute position the hotspots inside the same div. Inside the hotspots divs you will have the more info elements, showing only on :hover of their parents.
This makes it simple, and far more accessible.
Update: cropping the image equally from both sides
If you want to keep the image centered and still not use any javascript, you could set the required image as a background-image of the container, and setting its background-position parameters to center center.
You would have to make sure that the width of this div is set to the width of your image, and the max-width to 100%, so that when the window gets resized below the image width it stays at the center.
Now, a problem that I encountered here is how to make the hotspots stay center relatively to the image. I solved it this way:
I created a wrapper div for the hotspots with these characteristics:
margin: 0 auto;
position: relative;
width: 0px;
This basically makes sure that the wrapper div finds the center of our image. Then, you would position the hotspots relatively to the top-center position of the image, instead of the top-left as a starting point.
Then you have what you are looking for.
Working demo
Here's another approach, and in my opinion far superior to using a map or excessive JS. Place <div> elements on top of the element with the background-image and have HTML and CSS do the heavy lifting for you.
See it on JSFiddle
HTML
The HTML should seem pretty each enough to understand, we create <div>s with the class hotspot and rely on certain things being present. Namely .text (to show digit), .hover-popup (to show on hover) and .click-popup (which is inside .hover-popup and is shown when clicked).
<div id="hotspot1" class="hotspot">
<div class="text">1</div>
<div class="hover-popup">
I was hovered!
<div class="click-popup">
I was clicked on!
</div>
</div>
</div>
<div id="hotspot2" class="hotspot">
<div class="text">2</div>
<div class="hover-popup">
I was hovered!
<div class="click-popup">
I was clicked on!
</div>
</div>
</div>
CSS
This is where most of the magic happens, see the comments for further explanation.
/* These two position each hotspot */
#hotspot1 {
left:15%; /* we could use px or position right or bottom also */
top:20%;
}
#hotspot2 {
left:35%;
top:25%;
}
/* General styles on the hotspot */
.hotspot {
border-radius:50%;
width:40px;
height:40px;
line-height:40px;
text-align:center;
background-color:#CCC;
position:absolute;
}
.hotspot .text {
width:40px;
height:40px;
}
/* Show the pointer on hover to signify a click event */
.hotspot .text:hover {
cursor:pointer;
}
/* hide them by default and bring them to the front */
.hover-popup,
.click-popup {
display:none;
z-index:1;
}
/* show when clicked */
.hotspot.clicked .click-popup {
display:block;
}
/* show and position when clicked */
.hotspot:hover .hover-popup {
display:block;
position:absolute;
left:100%;
top:0;
width:300px;
background-color:#BBB;
border:1px solid #000;
}
JavaScript (with jQuery)
Unfortunately you're going to have to use some JavaScript for the clicking part as CSS doesn't have a 'clicked' state (outside of hacks with checkboxes). I'm using jQuery because it's dead easy to do what I want.
$(document).ready(function () {
$('.hotspot').click(function () {
$(this).toggleClass('clicked');
});
});
Creating the arrow
Over at css-tricks you can find a tutorial for attaching an arrow to a element using the :before and/or :after pseudo-elements. You can even 'simulate' a border around them by placing the :after element on top of the :before. But yea, lots of resources on how to do this.
You should be able to use the onclick or OnMouseOver event in the map area (define the href as "").
An example using OnMouseOver is here: http://www.omegagrafix.com/mouseover/mousimap.html
Give a class for that image in html (Ex: imgclass). And in javascript(using jquery), build that hover box in html format and bind it to 'mouseover' event of that image.
For example:
function bindhtmltoimage() {
myimg = $('body').find('.imgclass');
divSlot.each(function (index) {
$(this).bind('mouseover', function () {
try {
//position the hover box on image. you can customize the y and x axis to place it left or right.
var x = $(this).offset().left;
var y = $(this).offset().top;
var position = $(window).height() - ($("#divHover").height() + y);
var widthposition = $(window).width() - ($("#divHover").width() + x);
if (position < 0 || widthposition < 0) {
if (position < 0) {
$("#divHover").css({
position: 'absolute',
left: x + 20,
top: y - $("#divHover").height() - 20
});
}
if (widthposition < 0) {
$("#divHover").css({
position: 'absolute',
left: x - $("#divHover").width(),
top: y + 20
});
}
}
//build your html string for that hover box and apply to it.
$('#divHover').html("your Html content for that box goes here");
$('#divHover').show();
//if you want the box dynamically generated. create the html content and append to the dom.
}
catch (e) {
alert(e)
}
});
});
}
it will work fine in desktop and mobile. if you face any problem in touch devices, bind the function to click event instead of 'mouseover'.
Also, for map approach, i strongly recommend SVG instead of images.

How to set a target="_blank" in a DIV tag?

Got a page that displays some buttons (background images, etc) and they are all clickable. What I want this specific button to do is open the target page in another browser tab using *target="_blank"*. The way it is setup as the href in a div I cannot do this. Any ideas on a work around for this?
<div class="dashboard_navbutton" href="Home/RequestRedirect" style="background-image: url('#Url.Content("~/Content/images/Form_button.png")');">
<p>Insert witty text here</p>
</div>
Just make that div an a and add display:block; to the style.
EDIT: Ensure that your chosen DOCTYPE supports the use of p inside an a element. More generally, it should use the computed style for display rather than the tag name to determine if an element is inline or block in terms of having one in the other. I believe the HTML5 one is fine: <!DOCTYPE html>.
trap the onclick event for the div, call a javascript function, have the function openthe window.
html snippet
onclick="opennewwin()"
function opennewwin(){
var awindow = window.open(loc, "blank", "height=500px,width=500px");
}
I was trying to dynamically add divs that would also function as links.
This was my solution using CSS.
First the container needs relative positioning.
.container {position: relative;}
Next, the link needs to fill the container.
.container a {position: absolute; width: 100%; height: 100%; top: 0; left: 0;}
Like I said, I dynamically assembled the div, but the html would look something like this:
<div class='container'>[some other content]</div>
The container must be position relative, otherwise the position absolute link fills its first position relative ancestor (probably the whole viewport).
Of course, you can add styling to the div or the link. Note, I was using a position: sticky nav-bar, and I had to set it's z-index high in order to avoid collisions with the div buttons.
Pros: whatever styling and targeting you set for your links will apply. Good 'style': doesn't put a block element inside an inline (should avoid browser issues, though I haven't thoroughly tested it). Does not require any other languages or frameworks.
Cons: Not as simple as Niet's answer, but shouldn't be Doctype dependent.

Categories

Resources