Setting a custom center for a dialog in Angular Material - css

I'm trying to fix the position of my dialog in Angular. I can set or update the position using the DialogRef object or the MatDialogConfig object. When I set the left position to 25px the dialog will appear 25 pixels from left. When I do not specify a position the dialog will always appear in the center. Unfortunately, I'm using a sidenav which is 250px wide. So I would like my dialog to be positioned at the center + 250px. How do I approach this problem?
const dialogConfig = new MatDialogConfig();
dialogConfig.autoFocus = true;
dialogConfig.disableClose = true;
const dialogRef = this.dialog.open(TCtor, dialogConfig);
dialogRef.updatePosition({ left: "25px" });

You can use calc, a native css function to compute the left position of your dialog box. See more details about it.
The "less gracious" point is that you need to set the width of dialog box and use it also inside the calc computation.
So I would like my dialog to be positioned at the center + 250px
2 possible interpretations :
1) The left side of dialog box should be at center + 250px
Middle of screen: 50vw (50% of viewport width)
this.dialog.open(DemoDialogComponent, {
height: '200px',
width: '400px',
position: {
left: 'calc(50vw + 250px)'
}
});
2) Dialog is centered, but with a shift of 250px at right
Dialog box width: 400px
Middle of screen: 50vw (50% of viewport width)
this.dialog.open(DemoDialogComponent, {
height: '200px',
width: '400px',
position: {
left: 'calc(50vw - 200px + 250px)'
}
});

Related

How to recalculate top and left position when using background size cover on background image

I'm working on a website with a responsive background image where the size of that background-image is set to cover.
I also have a link with a hover animation which is positioned with top and left parameters on that background-image. Now my problem is that when my screen is resized I should also recalculate the top and left parameters to correctly show my animation.
The animation should be shown on a particular spot of the background-image. When viewed in resolution 1920x1240 the animation is positioned correctly. But when resizing the screen the position is not correct anymore because the image is scaled.
Is there a way to keep the top and left parameters in sync with the scale and how can I achieve that?
Currently I'm having this css for my background image:
div.custom-background {
background: url('../../assets/resized/de-sneeuwkoningin-xxl.jpg') no-repeat;
bottom: 0;
left: 0;
right: 0;
top: 0;
background-position: center top;
z-index: -1;
background-size: cover;
}
and for my animation I have the following css:
.start {
background: url('../../assets/de maanvallei_beeldmerk_boek.png') no-repeat;
background-size: 100%;
width: 130px;
height: 122px;
top: 45%;
left: 7%;
&:hover {
filter: drop-shadow(10px 10px 10px blue);
transition: filter 0.05s linear;
width: 130px;
height: 122px;
}
}
EDIT:
I made a fiddle to show the problem. The black dot on the forehead should remain on the same place when resizing the html view.
fiddle:
https://jsfiddle.net/3x2njkLu/
I'm providing some general tips that may help you, I hope my math isn't much off for what you need. You may need to adjust it, full implementation will take you a while
1. You need to have your image ratio stored image_ratio = 1920/1240 (~1.548).
2. Then you need to specify in what x,y relative points you want in ideal situation (both height and width equal to window size). Ex. x = x_coord/width=0.5 and y = y_coord/height=0.3 (50% left, 30% top).
3. Now observe how image behaves when scaling - makes next points differ from case to case. [Now considering current example view]
3.1. If rendered image dimension (x or y) = 100% of window dimension then dim_offset = 0
3.2. If rendered image dimension != 100% of window dimension then:
a) for height: y_offset = window height - window width / image_ratio and your new y: y_adjusted = y + (y_offset) / (height - y_offset)
b) for width: x_offset = window width - window height * image_ratio and your new x: x_adjusted = x (-+) (x_offset) / (width - x_offset) !! (-+) depends on left value - if y<50% then minus, if y>50% then plus, when 50% then it stays the same x=x
Your left value is x_adjusted and top is y_adjusted
Now I would recommend to use JS to accomplish those calculations with:
document.documentElement.clientWidth and document.documentElement.clientHeight
onresize event - https://www.w3schools.com/jsref/event_onresize.asp
for css: access style property of element
This post pointed me in the right direction: Keep div overlay relative to background image regardless of window size
I eventually solved my problem with the aspect ratio and css.
Thanks #Krysztof Nowicki for the tips

Div's height gets extended but items appear out of div's workspace

I want to do this simple task. I have the following code for div and ExtJS panel. DEMO
#drawing
{
position: absolute;
border: solid 2px green;
width: 100%;
height: 300
}
and
var contentPanel = Ext.create('Ext.panel.Panel',
{
title: '',
region: 'center',
margins: '2 0 0 0',
border: false,
autoScroll: true,
contentEl: 'drawing',
});
After the views are created, everything is rendered, I am calling some JS function that is supposed to resize div's height based on some condition.
I have yCoord variable that gets incremented within the program and once it has reached the height of contentPanel, I want to extents it's div's height with the following code:
if (yCoord > contentPanel.getHeight() - 200)
{
document.getElementById('drawing').setAttribute("style", "height:"+ (2*yCoord) + "px");
}
Simple, right?
But this is what I am having problem with...
Once the height is set, I can see the vertical scroll bar extended on the panel, "meaning that the workspace has been extended". But when you click 4rd time, the rect goes out of workspace. Why?
You forgot to resize your Raphael's canvas:
p.setSize('100%', 2*yCoord+contentPanel.getHeight());

Allowing Fixed DIV to continue to move horizontally

Fiddle: http://jsfiddle.net/EzuTT/
CSS
#bottomfadebar {
position:fixed;
z-index: 2;
bottom: 0px;
width:267px;
height:84px;
background-color:#666;
}
#content{
width:2000px;
background-color:#ccc;
}
HTML
<div id="content">
This is all of the data. Theres lots of it. so the user will have to scroll horizontally. the bottom bar should go out of view as you scroll further to the right because there's so much data. the bottom bar should only stay "fixed" to the bottom, not to the left hand corner.
</div>
<div id="bottomfadebar">
THIS SHOULD SCROLL HORIZONALLY AS THE PAGE DOES
</div>
Ultimately, the #bottomfadebar div continues to stick in the bottom-left hand corner as you scroll to the right to see more of the content div. I'm trying to figure out how to allow the bottomfadebar DIV to scroll off to the left of the screen, but still fix at the bottom of the window as it's currently doing.
------EDIT!!!
Ok, so I kinda blew it on this because I thought it would be easily explained but then I realized the absolute factor would come in. It actually should reside inside of a NAV div thats centered.
http://jsfiddle.net/u5GuG/4/
It DOES need to stick to the absolute left:0 inside the "container" area....I should have specified, I apologize. Not sure how to do that.
I would use a little jQuery for that, if you don't mind ;)
$(window).scroll(function() {
$("#bottomfadebar").css("left", (-1 * $(window).scrollLeft()) + "px");
});
Fiddle: http://jsfiddle.net/inti/Gqpmf/
Update: now I think I got it, you want the #bottomfadebar to scroll along the bottom of the screen while you scroll the page.
$(window).scroll(function() {
var window_width = $(window).width(),
window_scrollleft = $(window).scrollLeft(),
content_width = $("#content").width(),
bottomfadebar_width = $("#bottomfadebar").width(),
content_path = content_width - window_width,
bottomfadebar_path = window_width - bottomfadebar_width,
bottomfadebar_left = 0;
// Equations:
// content_pos = window_scrollleft / content_path;
// bottomfadebar_pos = bottomfadebar_left / bottomfadebar_path;
// content_pos = bottomfadebar_pos;
bottomfadebar_left = window_scrollleft / content_path * bottomfadebar_path;
$("#bottomfadebar").css("left", bottomfadebar_left + "px");
});
Fiddle: http://jsfiddle.net/inti/Gqpmf/2/
Update 2: I think I still donn't get it, but if you want it to stick to the [bottom,center] screen position, then this css is a go:
#object {
position: fixed;
bottom: 0;
left: 50%;
width: 200px;
margin-left: -100px; /* half of the width in negative */
}
Fiddle: http://jsfiddle.net/inti/Gqpmf/3/
Update 3: really my last guess. If you want an item to be absolute positioned inside another element and relative to it, you have to set the container element's position to relative (or absolute).
#container {
position: realtive;
}
#object { /* inside #container */
position: absolute;
left: 0; /* will stick to the left side of #container */
bottom: 0; /* will stick to the bottom side of #container */
}
I modified the code in your Fiddle.
I moved bottomfadebar inside of content, change the height of content to 100% and changed the bottomfadebar to absolute
http://jsfiddle.net/EzuTT/1/ - Is that what you are looking for?
Just switch bottomfadebar position to 'absolute'. As you already have 'bottom:0' set it will stick to the bottom of the page. It will not remain visible when you scroll horizontally as an absolutely positioned element will default to 'left:0' unless you specify otherwise (except in older versions of IE (7 and under I think) where you may need to declare 'left:0;' to avoid odd rendering.
Instead of using fixed positioning, use absolute and set your left and bottom attributes to 0.
This will position the div at the bottom left of the page, instead of the bottom left of the browser viewport.
#bottomfadebar {
position:absolute;
z-index: 2;
left: 0;
bottom: 0;
width:267px;
height:84px;
background-color:#666;
}
Fiddle is here: http://jsfiddle.net/u5GuG/3/

How do I set a div element's position fixed AND relative?

I would like to set a position fixed and relative. I have a large div for all my content. When i set the position of div inside to relative it aligns to the left of my content div. when i change that position to fixed it aligns to my browser side. I have been told that there is no css solution, but is there any other way.
basically im trying to get the same effect as the right hand bar on this website:
http://www.nytimes.com/interactive/us/sept-11-reckoning/viewer.html
Code:
#content{
width: 1000px;
margin-right: auto;
margin-left: auto;
}
#text{
position: fixed;
}
<div id='content'>
<div id='text'>Welcome to the site</div>
</div>
The function isScrolledIntoView() used below is from another question Check if element is visible after scrolling
Explanation of Example
First off we have the function isScrolledIntoView() which simply returns true or false if an element is visible inside your browsers viewing area.
You need to use two divs or some kind of anchor point and a follow div. Your anchor div is used to determine when we should switch the follow div to a fixed style to follow on scrolling or switch back to an absolute position if the anchor is back in view to return the follow div to its original position on the page.
With your anchor point, following div, the function to check if an element is visible in the viewing area, we can make use of the jQuery .scroll() event binding method on the window to fire an event when the user scrolls on the page.
Example Script
$(window).scroll(function () {
if (! isScrolledIntoView($('#anchor')))
{
// anchor div isn't visible in view so apply new style to follow div to follow on scroll
$('#follow').css({ position: 'fixed', top: '10px' });
}
else
{
// anchor div is visible in view so apply default style back to follow div to place in default position
$('#follow').css({ position: 'absolute', top: '100px' });
}
});
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}
Fiddle demo

CSS Fixed/Absolute positioned <div>

How do I put a div position: fixed; bottom: 0; until the scroll reaches the footer, then it's position: absolute; bottom: 0; of the content div.
I want the div to always stick to the bottom of the viewport, then stick to the bottom of the content div and not cover up my footer.
Bonus points for compass/sass solution!
In this fiddle (I used yours as a base, forked it though) you can see a working example of what you want, with a supposed footer height of 100px.
Here's the JS needed:
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
$('div#scrolled').css({'bottom' : '100px'});
} else {
$('div#scrolled').css({'bottom' : '0px'});
}
});
It checks, on scroll, if the scroll reached the bottom plus 100 pixels, and if so, it sets the bottom of the fixed element to 100px. The bad point is that it's not progressive, and the fixed element jumps when the footer appears. You cant just remove the 100 in the if statement if you want the footer to pop when the user reaches the absolute bottom: if ($(window).scrollTop() + $(window).height() >= $(document).height()) { ...
UPDATE:
Here is the "progressive" version of the above code.
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
var bottom = 100 - ($(document).height() - ($(window).scrollTop() + $(window).height())) + "px";
$('div#scrolled').css({'bottom' : bottom});
} else {
$('div#scrolled').css({'bottom' : '0px'});
}
});
Now, instead of changing the bottom to 100px whenever the user scroll is under 101px from the bottom, it calculates the position the fixed element should have depending on the scroll
Hope it helps!
Stick to position:fixed, and add margin-bottom:100px (when your div has a height of 100px) to your footer, so that that div doesn't cover up your footer.
An alternative (not recommended), would involve JavaScript, detecting whether scrollHeight == scrollTop + offsetHeight.

Resources