Show content on screen size - html

I am trying to create a banner that had 3 versions of 1 slide containing 1 image.
I know how to do this with display: none but that means all the content is loaded and it will effect load speeds on slow networks or mobiles.
I'm not a javascript or jquery wiz so the simpler the better please.
What i would like to do is:
on screen resolutions up to 620px
{Load this HTML }
on screen resolutions 621px to 920px
{ Load this HTML }
on screen resolutions 921px and above
{ Load this HTML }
Is this possible?

You could handle this with CSS screen queries but like you say it will load them all and hide them. So you would have to go down the route of JQuery me thinks.

I think the better way to do this's with CSS.
Per example, to make all images responsive:
img {
max-width: 100%;
height: auto;
}
check the another tricks>>
Or you can do this via #media's:
#media (max-width: 979px) {
(css here - widths < 979)
}
#media all and (max-width: 767px) and (min-width: 400px) {
(widths between 400 and 767)
}
#media all and (max-width: 399px) and (min-width: 0px) {
(widths between 399-0px)
}
Hope I helped.

"Is this possible?"
Yes, but in order to maintain code simplicity you'll need to use a different method: Send the user to one of three different pages depending on his screen width, instead of loading a new div in the same page. I'll explain how you can do it step by step. Here we go:
How to check the user's screen width with JavaScript:
if(window.innerWidth <= 620){
// The window width is less or equal to 620px.
}
if(window.innerWidth > 620 && window.innerWidth <= 920){
// The window width is greater than 620px and less or equal to 920px.
}
if(window.innerWidth > 920){
// The window width is greater than 920px.
}
You can also simplify this code a little bit if you store the window.innerWidth value in a width variable, like this:
var width = window.innerWidth;
Then you can use if(width > 620){/* Code here */} to check if the window width is greater than 620px.
Now you'll create two or more pages, each one with a modification and then send the user to another page if his screen size is X. To do so you'll only need to use location.href="#"; inside the above ifs and replace "#" with the other pages URL.
Handling page resize:
The above codes are examples on how you can check the user window width, but the browser will run this code when the page has loaded, one time only, but not when it's resized. To make this code run anytime the browser is resized you'll need to wrap these ifs in a function and call it using an EventListener, that'll say to the code when the browser is resized.
Final code result:
Here's the final code ported to a function, called inside an EventListener and sending the user to another page:
function checkWidth(){
var width = window.innerWidth;
if(width <= 620){
location.href = "#"; // The window width is less or equal to 620px.
}
if(width > 620 && width <= 920){
location.href = "#"; // The window width is greater than 620px and less or equal to 920px.
}
if(width > 920){
location.href = "#"; // The window width is greater than 620px.
}
}
window.addEventListener('resize', checkWidth);
checkWidth();
JSFiddle Demo

Related

How to hide a layout item and all its content on small devices

I have two layout items in a screen called (1) "AccountsMaster" and (2) "AccountDetails".
AccountsDetails contains many content items including a table.
I am trying to hide Accounts Details on smaller screens, such that only AccountsMaster is visible and using all the limited screen space.
I tried the following in user-customization.css
screen and (max-width: 400px) and (orientation: portrait),
screen and (max-width: 640px) and (max-height: 400px) and (orientation: landscape) {
.prols-phone-hidden{
display : none;
}
}
And then in my screen js code behind:
myapp.AccountsScreen.AccountDetails_postRender = function (element, contentItem) {
// Write code here.
$(element).addClass("prols-phone-hidden");
};
But that had no effect.
I also tried "visibility : collapse" in the css, well that did hide the layout and all its children, but the screen space it occupied originally was still reserved (and blank), so the master layout would not use the whole screen and will be squeezed in half of it.
How do I hide the details such that the master layout can have all the screen to itself??
Regards,
In Lightswitch HTML, I would do the following on your screen created event:
myapp.SCREENNAME.created = function (screen) {
//GET THE HEIGHT AND WIDTH OF THE SCREEN
var height = window.screen.availHeight;
var width = window.screen.availWidth;
if (height < 400 || width < 640) { //HIDE THE CONTENTS
screen.findContentItem("AccountsMaster").isVisible = false;
screen.findContentItem("AccountDetails").isVisible = false;
}
else {
//DO NOTHING
}
};
This 2 variables at the top will fetch your screen height and width, and then below you can determine what you want to do based on there values. by using the screen.findContentItem("") you can then hide the information you dont want to show because it does't display correctly.

Get html camera width and height

I'm seeing some code about capture video in html5
It works fine with this tutorial,(http://www.html5rocks.com/ko/tutorials/getusermedia/intro/)
but it differently work each device. phone and my notebook.
Because phone and my notebook's cam screen size are different.
So, I want to get each device's camera width and height, and adjust code with this value.
How can I get these values? or How can I get each device's whole screen value ?
you can use loadedmetadata event to get video height and width.
video.addEventListener("loadedmetadata", function () {
find_video_size();
}, false);
function find_video_size(){
if (isStreaming++ <= 10) {
// videoWidth isn't always set correctly in all browsers
if (video.videoWidth > 0){
cwidth = video.videoWidth;
cheight = video.videoHeight;// / (video.videoWidth / cwidth);
$("#tutcanvas").attr("width", cwidth).attr("height", cheight);
isStreaming = 11;
}else{
setTimeout(find_video_size, 200);
}
}else{
$("#tutcanvas").attr("width", cwidth).attr("height", cheight);
}
}

Is it possible to get the value of another elements in CSS

I'm creating a mobi site and would like to set the height of some of my elements to a multiple of the width, sorry if I'm not coming off clear enough here is an example of what I want to do.
#myDiv{
width : 40%;
height : width * 1.4;
}
I initially was just going to set the height to +-50% but I later realized I loose the aspect ratio depending on the screen size of the phone.
#cinnanon is right, this can't be done with CSS alone.
You could use jQuery (javascript), although this is probably not the best practice for building a responsive site:
var adjustHeight = 1.4;
$(document).ready(function () {
$('div').css('height', ($('div').width() * adjustHeight));
});
$(window).resize(function () {
$('div').css('height', ($('div').width() * adjustHeight));
});
http://jsfiddle.net/philsinatra/pqz59/

Prevent images from being downloaded to page on mobile site

How can I make it so that within the mobile version of my site the images are not downloaded to from the web server as these are large files that are not needed and not being used and therefore severely impacting the use of the mobile version of the site. Having looking at previous threads of such nature I saw that hiding the parent of the image using code such as below can benefit.
.parent {display:block;}
.background {background-image:url(myimage.png);}
#media only screen and (max-width:480px) {
.parent {display:none;}
}
The problem being I don't want to use background image CSS for SEO issues associated with them as I like to use Schema tagging etc ..so how can I prevent an IMG tag from being downloaded, as display:none; only hides the image rather than stopping it being downloaded.
Note: This is not for copyright protection issues e.g. preventing right click etc etc but for speed and ultimately size of the downloaded content to mobile.
This solution uses CSS to prevent background-images from loading and jQuery to prevent images from loading. I'm not familiar with any CSS solution that will prevent images from loading.
JS Fiddle: http://jsfiddle.net/CoryDanielson/rLKuE/6/
If you know the images height and width (or even ratio) ahead of time you could set the background-image for a bunch of fixed size DIVs. This might be applicable for icons and layout-type images. Look at the HTML/CSS below for an example of that.
Background Images
/* hidden by default */
aside {
display: none;
}
/* Pictures load for 'big screen' users.. pcs/tablets? */
#media screen and (min-width: 750px) {
aside {
display: block;
}
.catpicDiv {
height: 100px;
width: 100px;
display: inline-block;
border: 1px solid red;
background-image: url('http://img2.timeinc.net/health/images/slides/poodle-1-400x400.jpg');
background-size: cover;
}
}
and HTML
<aside>
<div class="catpicDiv"></div>
<div class="catpicDiv"></div>
<div class="catpicDiv"></div>
</aside>
Image Elements are a different story...
I don't know of any purely CSS solution to prevent them from loading the images. So I'd solve it like this:
Define IMG tags as follows
<img src="" data-src="url-to-image.jpg" />
Then, somewhere in the head of the document you need similar javascript
1) Function to load all of the images
function loadAllTheImages() {
$("img").each(function(){
$(this).attr('src', $(this).attr('data-src'));
});
}
2) Code to determine if the user is on mobile or a PC (slow vs fast connection) and then load the images.
This code isn't bulletproof, there are much more accurate and reasonable tests than this.
$(window).load(function(){
if ( $(window).width() > 750 ) {
loadAllTheImages(); // !
} else {
$("body").append("<a id='mobileCheck' href='javascript: void(0);'>I GOTS 4G, LEMME HAVE EM!</a>");
}
});
3) As well as maybe some code to activate a button to load the images anyways? Why not, I guess... ?
$(document).ready(function(){
$('body').prepend("<h1>" + $(window).width().toString() + "</h1>");
$('body').on('click', '#mobileCheck', function(){
loadAllTheImages(); // !
$("#mobileCheck").remove();
});
});
Similar solution as here and what I hypothesized in the comments:
Delay image loading with jQuery
There is no native solution in CSS that would prevent images from loading even if you hide them or set display to none.
You have to use some JS to achieve that result. If you are familiar with JS that should not be an issue at all. There are several plugins ready to go to do what you want. You can also write your own JS because its not that difficult.
Here is my code that loads images based on the screen size:
DEMO AT CODE PEN
It works without any libraries like JQ but if you use one of those it will automatically switch to it (Tweak it to your specific needs).
JS
// use jQuery or pure JS
if (typeof jQuery !== 'undefined') {
// jQuery way
// alert("jquery");
$(function() {
$(window).on('load resize', function() {
var products = $("[data-product-image]");
products.each(function(key, value) {
var bg = null;
if (window.outerWidth < 500) return;
if (window.outerWidth < 1000) bg = $(value).data("product-image-s");
if (window.outerWidth >= 1000) bg = $(value).data("product-image");
console.log($(window).outerWidth);
$(value).css({
'background-image': 'url(' + bg + ')',
'background-position': 'center',
'background-size': 'cover',
});
});
});
});
} else {
// Pure JS way
// alert("JS");
(function() {
window.addEventListener('load', wlImageLoader);
window.addEventListener('resize', wlImageLoader);
function wlImageLoader() {
console.log('event! Trig trig');
var all = document.getElementsByTagName("div");
var products = [];
for (i = 0; i < all.length; i++) {
if (all[i].hasAttribute('data-product-image')) {
products.push(all[i]);
}
}
Array.prototype.forEach.call(products, function(value) {
var bg = null;
var curent = window.getComputedStyle(value).getPropertyValue('background-image');
console.log(curent);
if (window.outerWidth < 500 || curent != 'none') return;
if (window.outerWidth < 1000 && curent == 'none') bg = value.getAttribute('data-product-image-s');
if (window.outerWidth >= 1000 && curent == 'none') bg = value.getAttribute('data-product-image');
// if (window.outerWidth >= 2000 && curent == null) bg = value.getAttribute('data-product-image-l');
if(bg == null || curent != 'none') return;
value.style.backgroundImage = "url(" + bg + ")";
value.style.backgroundPosition = "center";
value.style.backgroundSize = "cover";
curent = window.getComputedStyle(value).getPropertyValue('background-image');
console.log(curent);
});
}
})();
}
HTML
<div data-product-image="img/something_normal.jpg" data-product-image-s="img/something_small.jpg" id="p3" class="product">
However if you are a time loading freak you probably prefer to write your code natively in JS as you often don't use most of the jQuery library. For fast internet connection this is not a problem but if you target mobile devices on country side that might make a difference.
I would suggest combining perhaps the #import and #media commands to only #import the stylesheet which contains images if the #media tag meets you criteria (say, over a certain resolution).
So by default you wouldn't import the stylesheet which applies the BG image, you'd only end up doing it if you had determined the site was 'non-mobile'..if that makes sense!
The W3c site has some decent examples of combining the rules:
http://www.w3.org/TR/css3-mediaqueries/#media0

HTML & CSS - Create warning message for users with too low a resolution?

My website currently has a width too high to be viewed at 800x600 resolution or lower. What I would like to do is to create some kind of warning message, so when someone with a resolution this low they are made aware that the site will not view properly. Preferably, I would like a yellow bar to drop down from the top much like Internet Explorer does.
How can I do this by using HTML and CSS? If you can't, please tell me how to do it in any other language.
Thanks
.yellowbar {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
width: 100%;
height: 30px;
background: yellow; /* Use a better color */
/* and so on... */
}
#media screen and (max-width: 799px), screen and (max-height: 599px) {
.yellowbar { display: block; }
}
Do note however: older versions of IE do not support media queries.
If you want to use jQuery, you can use the following:
var $window = $(window), $bar = $('.yellowbar');
$window.resize(function me () {
var small = $window.height() < 600 || $window.width() < 800;
$bar.css('display', small ? 'block' : 'none');
return me;
}());
This'll obviously work even in older versions of IE.
Don't check for a resolution, check for a viewport size. Browser windows can be resized.
Get the browser viewport dimensions with JavaScript should get you started. Output the HTML you want if the viewport is too small.
I would use Javascript or JQuery rather for this.
var width = $(window).width();
var height = $(window).height();
if (width < 800 || height < 600) {
alert("Your monitor is from the dark ages.");
}