Prevent all of my contents of the page from resizing - javascript

I'm trying to prevent all of the content on the page from moving, resizing while width page is under 1350 px. Min-width doesn't work for this and i don't know why.. so what can i do ?
Btw on my css code, for the width, height, transformation: translate, i'm using the value vmax , and not px or %. So the min-width maybe doesn't work with it, all of my elements keep resizing because they are using vmax, so they just adapt there size with the window size too.. :(
I tried this too, but still don't work :(
body {
min-width: 1350px;
}
The code + preview
(I'm trying to stop resizing the three white rectangle in priority when my browser width is under 1350px, but if we can tell to the page to stop resize all of the content when she is under 1350px, and only use a scrollbar to deplace the elements on the page, it will be cool !)
See this page for exemple , when she is less than some px, the page stop from resizing and a scroll bar appear, i want the same result but i don't know how to do it ...

you are using css dimensions that are relative to the viewport. vmax vmin, vh, vw are relative to the viewport and thus react to resizing of the window. if you want fixed dimensions, you should use em or rem
edit: if you want to prevent the white boxes from resizing, you could add a min-width: 450px so they can grow but don't shrink beyond a point

vmax is not related to document size, but to actual browser size. Maybe absolute units like px, rem, em are a better solution in this case.

For contional css, based on the specific width of the window, you can use media-queries:
#media only screen and (max-width: 1350px) {
body {
background-color: lightblue;
}
}
That way, you'll be able to define sepcific rules for each resolution / rule that you want.

Related

How can I scale my webpage proportionally using inner width to fit any display size?

I want to do the scaling depending on only width. I don't wanna mess with height. I have a website which unfortunately I made it for a way bigger display size. I didn't know how the adobe xd artboard size was too big. I thought it was 1920px but actually the width of my website is 3554px. I want to scale it down to fit smaller screens and scale it up for bigger screens. I can only make it fit in my laptop screen but I don't know the calculation behind for how to make it work for any screen. Basically I want scale to fit width and center the page horizontally. So everything is kinda based on width. The base width is 3554px. Please help me on how to do this using plain javascript?
Please check this link. Want to do something with plain javascript but using only width and keeping it at center position.
Proportionally scale website to fit browser window
CSS
#Web_1920__1 {
position: absolute;
width: 3554px;
height: auto;
border: 0px grey solid;
background-color: rgba(255,255,255,0);
overflow: visible;
visibility: visible;
}
This is the main div that should take the whole inner width.
JAVASCRIPT
const mypage = window.innerWidth;
document.getElementById("Web_1920__1").style.transform = "scale(0.432)";
document.getElementById("Web_1920__1").style.transformOrigin = "0% 0%";
I can get the inner width but those transform properties only work for my particular screen. How can I get the transform values dynamically to fit the page on any screen and keep it at the canter? No need to do anything with the height because there will be scrollbar.
Abhay Srivastav's solution is working fine. A lot of thanks to him. The solution is like below. It's proportional scaling depending on width only. Everything will remain horizontally centered. Upscaling downscaling works for screen size. Window resize also works fine.
JAVASCRIPT
document.addEventListener('DOMContentLoaded', updateMe);
window.addEventListener('resize', updateMe);
function updateMe()
{
const mypage = window.innerWidth;
document.getElementById("Web_1920__1").style.transform = "scale("+mypage/3554+")";
document.getElementById("Web_1920__1").style.transformOrigin = "0 0";
}
This piece of code can save you from putting additional thousand lines of plugin code. Page becomes lighter, loads faster and significant performance improvements.

Trying to achieve some kind of scrollable container with dynamic height

I have <div> with some dynamic content inside - sometimes it is table, sometimes it is text. I'm trying to setup some kind of container which would be scrollable on y-axis if its contents are too big for the screen.
Some googling suggests that its simple to do if you hardcode height of the div, but what should I do if I want to avoid specifying exact pixel sizes anywhere?
From what I understand, I'll have to calculate remaining vertical visible screen space first, then set it as div height. This seems like a fairly common task, yet I can't find any components(don't care if they are jquery dependent or not) that would accomplish this.
Any suggestions? Already existing solution(plugin, library, etc) is preferred.
If you would like the container height to start scrolling when it matches the viewport height, you can just give it height or max-height equal to 100vh (100% viewport height).
In case there are other elements outside of the container and it shouldn't take the full screen height you can use calc() to limit it:
.container { max-height: calc(100vh - 100px); }

Particle slider mobile resize not working

I am using this particle slider on my page: http://particleslider.com/
It works good, but on mobile devices it is not resizing. It just centers in the middle of the screen depending on the device size. Particle slider uses image URI to convert an image into particles. If the image width is larger than the phone width, it won't scale down - it overlaps horizontally like this:
How can I make it scale down so the entire image can be shown, irrelevant of device width?
It seems to work on the Particle website. Did you mess with the width or height of the image that might be conflicting? Do you have an example of your CSS, HTML and JS?
In their CSS, they are setting the outside container of the element does not have a width but a max-width of 95% in the CSS.
Did you set a width in the JS as an option?
According to their documentation that sets a static width. Default is 800.
// Create a 100px wide ParticleSlider.
var ps = new ParticleSlider({
width: 100
});
Most likely you could use CSS to fix your issue.
First, if you are, don't put a width in the ParticleSlider() as an option.
Second, force the max-width of the container element of your slider to be something like 95%.
Their CSS shows something like this for theirs
#particle-slider {
width: 41.8em;
height: 26.3em;
margin: 0 auto;
}
If you have code, please post it.

Keep the all the elements at the same place

I'm wondering because I am coding through multiple screen sizes and making a game that requires event.pageX and smaller screen sizes mean squishing everything as well as bigger screen sizes that push everything outwards. Is there a way to keep everything all positioned into one place so if you change screen sizes it is still positioned the same?
There are few css tricks that can helps you with your problem:
You can define body width and height, that will prevent window changes on resize, for example:
body {
width: 800px
height: 800px
}
Avoid using percents in element position defenition
Css position : absolute can make your element behave independently from other elements. position : fixed allows you ignore even user scroll if you need this. (more info)
small jsfiddle with some examples:
https://jsfiddle.net/qc3p2d5h/2/

Javascript mobile - calculate element dimensions in pixels based on screen dimensions

I want to calculate the dimensions of certain elements (img, ul, div, etc.) based on screen size. I can't to use percent values. I need pixel values. I also don't want to 'hardcode' everything using media queries and a new set of images for every resolution or screen size.
I thought about making this using screen size. I only need width calculation. So I add the initial width of my images and some initial space between them -> total width, and I then get scaling factor using: screenwidth / totalwidth
Now I scale all of my images and also the space between with this factor.
It's a very simple layout, only a few images and HTML elements. So this scaling should not be expensive.
It would work if the devices gave me reliable width measure for the screen. But depending of the device, I get a different meaning of this value. I'm using screen.width
In some cases screen.width is what the currently width is - in portrait it's a small value, in landscape a large one. But in other ones, width is always the same - the value which is defined as device's width.
So how do I scale my layout according to what's currently screen width in a consistent way with rotation, and without CSS % values? Is this an acceptable way to do layout scaling or am doing no-go?
Edit: I have to add more details after trying Jasper's solution. The images are used in a slider. The slider is basically an UL and each LI contains an image with float:left - so all the images are appended horizontally one after the other. With overflow hidden and stuff only the current slide is visible. Now, the official width of the UL is the sum of the width of all contained LIs. And this means, at least with my current state of knowledge, that I can't use percentage size for the LI elements, because if I did, this will be % of this total width of the UL, which is very large, and I end with immense LI elements/images.
Isn't there any reliable way to get current screen width for all devices ? I already have working code, I only need that the value of screen width is correct.
New update
Look here is a similar approach to what I'm trying to do:
http://ryangillespie.com/phonegap.php#/phonegap.php?
Entry of June 18, 2011 "One Screen Resolution to Rule Them All"
I tried also with exactly that example, copy pasting it in my code. But it doesn't work either. window.outerWidth has the same problems as I'm describing for screen.width (as well as JQuery $('body').width()). It works as long as the device isn't rotated - it initializes well. But at the first rotation, depending of the device, I get problems. In some it works as expected, in others it interchanges the values, so that I get large width in portrait mode and short in landscape, in others it gives fixed width and height all time, in others it doesn't rotate at all....
This is most likely accomplish-able with CSS alone (which is usually good for performance):
img {
width : 100%;
height : auto;
}
That will keep all the image's aspect ratios but re-size them to 100% width. Now that width is set based on the image's parent element(s) width. If you are using jQuery Mobile then the data-role="content" elements have a 15px padding, so to remove that you can just add a container to the image elements that removes the padding:
HTML --
<div class="remove-page-margins">
<img src="http://chachatelier.fr/programmation/images/mozodojo-mosaic-image.jpg" />
</div>
CSS --
.remove-page-margins {
margin : 0 -15px;
}​
And walaa, you've got responsive images without loads of code or overhead.
Here is a demo using a container and not using a container: http://jsfiddle.net/EVF4w/
Coincidentally I found that this works:
$(window).resize(function() {
updateScaling($('body').width());
});
This is always called and passes correct width. As far as I remember it also works with screen.width
In updateScaling I calculate a scalingFactor and adjust my elements.
I tried out responsive CSS, media queries and so on, but at some point it didn't make sense anymore, because I have anyways to recalculate the margin of slider's UL based on current slide and new width - and other stuff which needs script. So I made everything with script.
I removed window.onorientationchange.

Categories