HTML/CSS/Javascript/Center - javascript

have the following;
I'm displaying a 5000x5000 px image.
(it is a floor plan)
You can scroll over the image using the horizontal and vertical scroll bars
and the mouse pressed wheel.
at various points I have light points, if you press the button a popup will appear, letting you turn on or of the light or even dim.
to give you guys an idea.
Oke now I want this popup to be centered in the middle of the screen, NOT to the center of the image. Because if the view is top left, the popup would not be visible. visa versa with the lower right.
As the code is generated by an design studio I do not have many options.
An answer would be nice.
regards,
Ger

The following will center a .popup div on the page, regardless of where you scroll or what parents the popup has.
.popup {
position: fixed;
top: calc(50vh - (**INPUT POPUP HEIGHT**/2);
left: calc(50vw - (**INPUT POPUP WIDTH**/2);
}
In case you don't know the exact height or width, there's a less clean version involving setting the popup margin to auto and it's parent to display: flex. Look at flexbox if you're having trouble with this.

Related

Animate div from middle of div to bottom right of screen into a fixed position

What I am trying to figure out is how to animate a div that will start out in the middle of a div that is in the middle of a page. The div originally should not have a position: absolute. Unless it is not possible, I would like it not to start with that because it seems very tough to have any data below it. It's not going to be that big of a box. I am guessing anywhere between the height of 100px and 600px, with a width between 400px and 800px.
I originally found this JsFiddle Example that does a great job and almost exactly what I need. But the div starts with an absolute position and it is already located at the bottom right of the page to be animated.
Once the div is at the bottom right of the page, it needs to be fixed there so that I can scroll up and down the page without it moving. At this point I am not worried about being able to push it back up to the spot in which it came.
A couple things I tried: Lining it up in the position I desired, and then on the click of a button, add a class with the attribute position: absolute and calling the animate function like this:
chatWindow.stop().animate({
bottom: 0
, right: 0
}, 2000);
But my guess is that it originally needs to the the position set as in top: 0; left: 0 and that's why it won't work.
I already have it working without any animation and would love to be able to figure out how to animate this thing. Without animation, it's as simple as toggling a class with it's normal positions attributes with one that has a position: fixed; bottom: 0; right: 0.
Here is a Codepen Example that I created to show really what I need other than right animation part not being there. Any help would be awesome as I've been toying with this for quite some time now.
If you want an animation from left to right, you will have to play with left and top values. But the negative point is that will cause a weird animation because you want to keep a relative position of the box in the beginning.
So when you will do the animation, it will start from the very top left on the window, which is not good.
Like this
To avoid that, you will have to use absolute position in the beginning state. You said in your question you doesn't want it but I think it is required to get the wanted visual effect.
See here the live example with good effect
However, to keep a pretty nice animation, but I know it is not what you want, you can play with right and bottom values. It will make the box appears from the right and bottom corners of the window.
Like this
One possibility, still using absolute positioning, based on what's going on in your codepen example, would be to fake the current positioning by adding the following CSS:
.container {
padding-top: 250px;
}
.center-window {
position: absolute;
right: 50%;
margin-right: -200px; /* i.e. half of its width */
bottom: 100%;
margin-bottom: -250px; /* i.e. its height */
}
Then you could animate the right, bottom, and margin properties accordingly. See https://codepen.io/anon/pen/RaOJYY (though it doesn't currently do anything with the padding). Of course, if your not sure of the dimensions of .center-window, perhaps this solution won't quite work.

jquery resize so content is always centered perfectly

I have the following right now, but its not perfect:
$(window).resize(function () {
$("#app-views").height($(window).height() - 140);
});
Basically, I have 75px from top before my content starts, and I have 60px from bottom of the page to my content.
How do I make it so when I resize the window, it will always respect those dimensions? I am using malihu scroll bar, and I am loading my view into #app-views.
I have a border all around the window (10px), a navbar (50px), and 15px of padding until my body. Then, I have 15px bottom padding on body, a footer of height 35px, and 10 px bottom border.
Here is the basic HTML:
If you want your contents to be placed and resized while keeping the same distance from the top and the bottom of the window, you don't have to use jQuery or Javascript. Only CSS would do the trick. Try this without height attribute in your style code:
#app-views {
position: fixed;
top: 75px;
bottom: 60px
}
You can set left and right without width to get the same effect in horizontal dimension.
You say you have specific measurements to place your content on the page
(75px from top before my content starts, and I have 60px from bottom
of the page)
Well with jQuery offset you can get the top position of the element and you can also update the css top position on screen resize so that your content will always adjust its position on resize.
To see where the bottom of your content element is you could find the offset of the top of the content and add the content's height to get the bottom position of the content relative to the top of the page.
I would recommend doing this in CSS, perhaps by dynamically changing the jQuery object's CSS property. I would attend to it with a simple CSS selector. This works even when the window is resized. Have a look:
#app-views {
position: absolute; /*this will allow you to position it exactly where you want it*/
left: 50%; /*this will move the left side of the container halfway across the page*/
transform: translateX(-50%); /*moves the container left by half its width,
so that the centre of the container aligns with the center of the page*/
}
You can adjust the vertical position with the 'top' property and 'translateY()' in a similar way I demonstrated with transform and translateX().
If you want to use jQuery, you could try:
#('app-views').css('position', 'top');
Furthermore, I would also suggest that you do not maintain the 75px at the top of your page for all kinds of screen sizes. 75px may be suitable for a desktop but not for a mobile. If you do intend to make your website fully support mobile, it is often a good idea to design the mobile layout first, as it tends to by simpler. Then, you can use media queries to adjust it for the desktop. It really does work brilliantly. I've used it myself many times before. You can learn more about that here:
MediaQuery CSS

Center div vertically or affix to top of window if height is too great

I have a popup that's centered on my page using the following syntax:
position:absolute;
top: 50% !important;
left: 50% !important;
transform: translate(-50%,-38%); //well, slightly lower than center...
This works very well - unless the height of my popup is greater than the height of my current window. It doesn't happen often, but when it does, I can scroll DOWN to see the additional content, but I cannot scroll UP any further than the top of the window to see what's above, like in this example:
I want to say vertically center, but if the height of the div would cause the top of it to not be displayed, just affix the div to the top of the window instead
What code would I add into this to achieve that?
This is similar to another post on SO:
How to center an element vertically in css in a scrollable container
I'm not looking to center the item with a flex container however. I actually cannot use flex in this instance, because the div is a result of a javascript plugin and I can't add a separate parent div outside of this without a LOT of work. I am looking to center UNLESS the height of the div is too great - then i want it to affix itself to the top of the window.
If I could place some sort of div above this popup div that has a height of 1px and force the popup div never to go higher than it, that would be fine as well. I have a very specific reason for why I want the div at the TOP of the window when it's very long - I don't want it centered 100% of the time (which is what the other post does.)
I've also tried using a pseudo-element like so:
.popUpBody::before {
content: " ";
height:50px; //this needs to be dynamic based on the height of the parent
width:100%px;
display:block;
}
as it moves the rest of the div down - but I'd need to find some way to use a dynamic height - it might be 300px, or it might be 5000px. This also doesn't seem like a very good way to do this.
I could make it so the height of popup div is always centered by taking out the second transform variable: transform: translate(-50%); - and this would be a last resort, but I would really like for the div to affix to the top of the window when its height is larger than the window itself - if this is possible.
Javascript/Jquery is fine if I need to use that to add dynamics.

why does part of the content scale on this slider

I have used THIS tutorial to create a full-screen version for a website I am working on.
As you may have noticed I have succeeded in making most of the changes I needed.
http://coolcarousels.frebsite.nl/c/68/
But I am unable to figure out why the cans that shrink and grow every time the slides are clicked and made active.
JSBin
I have a feeling it has something to do with the Slides changing their width when going from active to smaller slides, But I do not understand why should the content grow/shrink with it.
// resize currentslide to small version
currentSlide.stop().animate({
width: _width * 0.065
});
Is there anyone that can help me understand why this is happening?
All I want is for the click to reveal the slide and the cans to stay the size they are suppose to be. Just like the original tutorial a clean reveal to the content.
The reason the cans expand is not a JavaScript issue, its because the CSS for the can is width:100% when its a small slider and when the new slide is clicked, the active slide goes from 60% to 10% and therefore the can takes up 100% of it.
.slide .can {
position: absolute;
width: 150%;
left: -56%;
bottom: 0;
right:auto;
}
The easiest solution would be to turn the values into pixels so that it stays the size it is while transitioning. But that perhaps complicates responsive behaviour.

Slider from the left side of screen to the right side of content

This is my first post so hello! :)
I have a problem with coding my site!
May I show it on this picture:
I want to make a slider (with captions for each slide) which is aligned to the right edge of content div.wrap. But left side of it must by to the left side of the screen. It must change to the screen resolution (always to the right edge of the content div.wrap Something like this
Right edge of div.wrap must be a limit.
I have no idea how to do this. The slider must be a background fader? Or img fader?
Please help me or show me how to do this on similar example.
Further to #Itay's answer, here is a jsfiddle which might help you with the CSS for this.
http://jsfiddle.net/mmWq6/4/
I've used the example of a slider that is 500px by 200px. This is what I guessed would be a miniature version of the webpage I expect you're creating, if you scale the 'Result' window narrower and then wider again you'll see that I think the design works how you want it to.
This demo basically demonstrates Itay's comments, which were to have a wrapper (#slider-wrapper) which had the CSS position: relative and a div within that with the CSS:
#slider-wrapper-inner {
position: absolute;
right: 0;
}
Then within that are .slides which are relatively positioned and .captions within them that are positioned absolutely, as above (but with an extra line: bottom: 0;), so that they sit in the bottom right.

Categories

Resources