Make top image stay in position when main image resizes - javascript

I have a main image and little callout buttons on top of the main image. I'm trying to get the callout buttons to stay in position when the screen size changes while the main image size resizes with max-width and background-size: contain, but right now, the callout button's position changes.
Here is the CSS:
.main_image
{
background: red;
height: 400px;
background-size: contain;
position: relative;
max-width: 100%;
}
.callouts
{
background-image: url(http://www.autotintspecialist.com/zoomButton_moused.png);
height: 70px;
width: 40px;
height: 30px;
position: absolute;
top: 70px;
right: 180px;
}
Here is the HTML:
<div>
<img class="main_image" src="http://autotintspecialist.com/sOff_off.jpg">
</div>
<div class="callouts"></div>
Here is the link to the fiddle: http://jsfiddle.net/Nem2C/
As you resize the image, the callout button changes position, but I need it to stay where it's at when the image resizes.
is there a jquery solution or a javascript solution to this as well?

I tried to fix this. I didn’t finish, but I got closer to the solution.
See this jsFiddle. I put div.callouts inside the div with the image and positioned it relative to that with percentage values.
<div class="image_wrapper">
<img class="main_image" src="http://autotintspecialist.com/sOff_off.jpg">
<div class="callouts"></div>
</div>
.image_wrapper {
position: relative;
}
.callouts{
top: 15.5%;
right: 24.1%;
}
To calculate the correct percentage value from, for example, the top, use the formula (image_height_in_pixels / pixels_from_the_top) * 100.
The problem with what I have now is that the callout x-position doesn’t scale with the width properly for some reason.

Related

Two images on each other with responsive behavior and proportional scaling

I would like to do something pretty standard in HTML/CSS/javascript I think but somehow I didn't find any solution for now: I want to have multiple images on each other with some of them being clickable. For example:
submarine with red circle button as window in this case the submarine is one img and the red circle is an input type="image" working as a button.
I want those multiple images to behave "as one" in term of responsivness and scaling so that I still see the same overall image independantly of the size of my window.
If I use this trick here: How do I position one image on top of another in HTML? and make both images responsive then the circle is not scaling down simultanuously with the submarine. Moreover, since the red circle is positioned in an absolute way it is not staying at the same place relative to the submarine.
here is my current code:
.div{
position: relative;
}
.responsive {
max-width: 100%;
}
#test2 {
width: 12.3%;
position:absolute;
z-index: 2;
left: 73%;
top: 62%;
}
#test
{
width: 100%;
position:relative;
z-index: 1;
}
<div>
<img src="/submarine.png" id="test" class="responsive" />
<input type="image" src="/red_circle.png" id="test2" class="responsive" />
</div>
In order to achive that, you can work with percentages, so if you reduce the scale of the window the size of the images reduce as well.
CSS:
.submarine {
width: 30%;
height: 55%;
position: relative;
}
.redDot {
width: 2%;
position: absolute;
}
HTML:
<div>
<img src="submarine.jpg" clas="submarine">
<img src="redDot.png" class="redDot">
</div>
Then play with the margins in orther to position the red dot in the submarine.
Dimensions and positions in percentages relate to the dimensions of the parent element. In your case the window of the submarine should be positioned as a percentage of the submarines dimensions. What you should do to make this is work is to put the window as a child in the submarine. Easiest would be to work with divs with background-images and use background-size: 100% to make the background-images scale with the elements.
Also you could use the "padding-bottom trick" to set the "height" of the div to a percentage of the parent's width.
#submarine {
background: yellow;
width: 30%;
padding-bottom: 20%;
position: absolute;
left: 20%;
top: 20%;
}
#window{
position: absolute;
background: red;
width: 20%;
padding-bottom: 20%;
right: 5%;
top: 40%;
background: red;
}
<div id="submarine">
<div id="window"></div>
</div>

Display image in full browser window but only scale down if image is too large

I want to display an image centered within the entire browser window. There are a few conditions though. If the image size fits within the browser's client area, display it at its original size. If the image is taller or wider than the browser client window, then scale the image down. Finally, if the user resizes the browser, the image will either scale down (if too large) or scale no larger than its original size if the browser window exceeds the image size.
I can do all this with jQuery but am wondering if it can be done using css alone?
EDIT:
The closest I've got is this:
https://jsfiddle.net/Deepview/o5vgo6du/
html
<div class="outerCont"> <img src="http://www.wonderslist.com/wp-content/uploads/2015/10/Doutzen-Kroes-Most-Beautiful-Dutch-Woman.jpg" /> </div>
css:
.outerCont {
position: relative;
}
img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
max-width: 100%;
height: auto;
width: auto;
}
But the image doesn't center vertically.
Had to add the following to the outer div:
width: 100vw;
height: 100vh;
https://jsfiddle.net/Deepview/o5vgo6du/3/
Is that what you want to do? Just use background-size:cover and it will always fill the whole div.
*{
margin:0;
padding:0;
}
.image{
background:url('https://www.aussiespecialist.com/content/asp/en_gb/sales-resources/image-and-video-galleries/jcr:content/mainParsys/hero/image.adapt.1663.medium.jpg') no-repeat;
background-size:cover;
width:100vw;
height:100vh;
}
<div class="image"></div>

How to use an image as a frame, and scroll inside? Simulating a phone on a website

I'm not sure what this is called, but how do developers accomplish being able to have, say a hollow image of a Nexus phone, and then scroll content inside of it? It's an ingenious way to simulate how a product will work in real life, so how does one pull this off?
Here's an example, about halfway down the page.
http://grupoweb.upf.edu/innova/q_kly/#step-6
#silversunhunter
I am able to get both images displayed, but the content seems to be completely obscuring the parent div. I measured the images and the dimensions are correct afaik.
css:
.nexus5-frame {
background: url(img/nexus5frame.png);
height:640px;
width:326px;
position: relative;
}
.nexus5-content {
overflow: scroll;
height: 515px;
width: 292px;
position: absolute;
left: 26px;
top 597px;
}
HTML:
<div class="col-lg-5 col-lg-offset-2 col-sm-6">
<div id="nexus5-frame">
<div id="nexus5-content">
<img src="img/content.png"/>
</div>
</div>
</div>
You need to set the image as a background image in the parent div. Then a properly measured div inside that is absolutely positioned can be your scrollable content.
<div id="main">
<div id="content"></div>
</div>
the phone image is 345px × 661px
#main {
background: url(/filelocation) no-repeat;
height: 661px;
width: 345px;
position: relative;
}
the screen is 305x601 (hypothetically)
#content {
overflow: auto; /*this gives us our scroll bar when the content is longer than the div*/
height: 601px;
width: 305px;
position: absolute;
left: 20px;
top: 30px;
}

How to continue the background of a div on another div/span?

In my webpage I have have a block with an background and below I have a button that should continue the background.
Here is an example. The left image is what my webpage is now, and the right image is what the webpage should be. You can see that the background continues on the button.
My code structure is something like:
<div id="section-1">
<div class="shown-content">
<div class="background"></div>
<div class="content">
... here are shown contents ...
</div>
</div>
<div class="hidden-content">
... here are hidden contents ...
</div>
<div class="button-content">
<span class="button">FOLD OUT</span>
</div>
</div>
The functionality is that when you click on the button, it triggers the JQuery slideToggle and it shows/hides the hidden-content div.
My idea is to set the button background the same width and height than the content background and then position it where appropriate. But I'm a bit lost because I don't find any way of doing this, and maybe you know a better way.
Let's say your placeholder for the image is 100px in height and the button is 30px.
Let's say your button always are in the center of the main image div.
Then you need an image that is 130px high, where the background position is set to center top and the buttons background position is set to center bottom.
Sample
.imgdiv {
background-position: center top
}
.buttdiv {
background-position: center bottom
}
If your button isn't in the center you need to adjust the "center" part of background position to make it match the main image
Your initial idea is probably the best solution.
Use background position to correctly position you div.
.button-content{
background: url('') no-repeat 200 100%;
}
Numbers afer no-repeat are X position Y position of background image.
I've found this solution, hope it will help you.
Place the fold out button absolute in the relative positioned #section-1.
While placing it absolute, it will only take the width it needs. Then we use the pseudo-classes :before and :after to fill the space on the left and the right with the background color of the body (in my example white).
The HTML looks like this (expandable with your rest code):
<div id="section-1">
<div class="hidden-content">
hidden content
</div>
<div class="button">
Fold out
</div>
</div>
And the CSS:
#section-1 {
min-height: 100px; /*use min-height so it will expand */
background: url('pic.jpg');
position: relative;
padding-bottom: 30px; /* height of .button */
overflow: hidden; /* to hide the :before and :after */
}
.button {
position: absolute;
bottom: 0;
right: 20px;
height: 30px;
width: 75px;
text-align: center;
}
.button:before, .button:after {
content: ' ';
background: white;
position: absolute;
width: 1000px;
left: -1000px;
height: 30px;
}
.button:after {
left: 100%;
}
.hidden-content {
display: none;
}
And a demo.

Calculate apsect ratio width based on height

I have an inline img that is 1280(w) x 1024(h) and is contained within a div. The divs dimensions are set 100%(w/h) of the viewport with overflow hidden.
I want to use the image as a fullscreen background and require that it fills the viewport completely regardless of dimensions with no borders showing. It must keep its aspect ratio and I would also like the image to be centered at all times. I am aware that to achieve this some of the image will be cropped.
I believe if make the image height match the viewport height and calculate the width based on the images aspect ratio this will work but I am unsure how to do this. Can anyone help?
Many thanks in advance.
Use backstretch http://srobbin.com/jquery-plugins/backstretch/
$("#demo").backstretch("http://urltoimage.jpg");'
set #demo to 100/100% width and height
If you're not 100% glued to using an img tag, you can do the following:
HTML
<div id="background"></div>
CSS
#background {
background: url('path/to/img.jpg');
background-size: cover;
background-position: center center;
width: 100vw;
height: 100vh;
}
Not really an answer to your question but a potential alternative -- have you tried the min-height/min-width CSS properties?
HTML:
<div class="container">
<div class="image-wrapper">
<img src="..." />
</div>
</div>
CSS:
.container {
width: 100vw;
height: 100vh;
background-color: grey;
}
.image-wrapper {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.image-wrapper img {
position: absolute;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
transform: translateX(-50%) translateY(-50%);
}
Fiddle: https://jsfiddle.net/mkjoyep1/
In the example, .container (your full width/height div), fills the page width vw/wh and the .image-wrapper has its width/height set to 100% which will match the parent's width and height. With overflow: hidden acting as the crop mechanism you can then stretch your image to fill it's container. I've positioned the image in the center with the method explored here.
This appears to work for me on Chrome and Firefox.
*Edited to include html/css

Categories