Responsive design + jQuery fine except Firefox - javascript

I'm using some pretty basic jQuery to force things into position when they are moved around:
function ipad() {
var width = jQuery(window).width();
if (width < 1200 && width >= 768){ //if tablet
jQuery('.mobbut').css('width', '33.333%');
jQuery('#re2').css('max-width', '');
} else {
jQuery('.mobbut').css('width', '100%');
jQuery('#re2').css('max-width', '400px');
}
if (width < 768){ //if mobile phone
jQuery('._btnselect').hide();
} else {
jQuery('._btnselect').show();
}
}
jQuery(document).ready(function() {
ipad();//run when page first loads
});
jQuery(window).resize(function() {
ipad();//run on every window resize
});
jQuery(window).load(function() {
ipad();//run when page is fully loaded
});
So far my website is perfect on Chrome, Safari, iPad, iPhone, but for some reason it looks a mess when you resize your Firefox window to smaller than 1200px wide. Try for yourself here's the webpage. Is it something to do with the jQuery or more the layout of the page? I inherited this homepage from another designer and it was previously built on tables so this may be giving FF problems.

I tried using media queries and they didn't work, I had to turn to jQuery to get the results.
If they didn't work, it's probably because the syntax was incorrect. Try it like this, which is pretty much exactly what your jQuery is doing, but much simpler:
#re2 {
max-width: 400px;
}
.mobbut {
width: 100%;
}
#media only screen and (max-width: 767px) {
._btnselect {
display: none;
}
}
#media only screen and (min-width: 768px) and (max-width: 1199px) {
#re2 {
max-width: none;
}
.mobbut {
width: 33.333%;
}
}
--EDIT--
Since it looks like you're trying to target the iPad. Try these media queries, from this post:
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* iPads (landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}
/* iPads (portrait) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
/* Styles */
}
These will likely work even more reliably than your jQuery adjustments since they are based on the device-width rather than the page width.

I ran your site with firebug (I do recommend using it) enabled and the error is in the following function
** function futsal(){
var wid = jQuery(window).width();
if (wid <= 1024 && width >= 768){ //if tablet **
just there you declare the var wid and then test against width
I agree with all of the comments above though, use media queries. It's far easier.

Related

window.matchMedia triggers too often (false positives)

I’m setting up multiple media queries like:
for (var layoutId in layoutList) {
console.log('Set up MediaQuery [%s]:', layoutId, getCssMediaQueryDefinition(layoutId));
var mql = window.matchMedia(getCssMediaQueryDefinition(layoutId));
mql.removeListener(onMatchMediaChange);
mql.addListener(onMatchMediaChange);
}
var onMatchMediaChange = function (event) {
console.log('onMatchMediaChange [%s]:', _.get(getLayoutForCssMediaQuery(event.media), 'id'), window.innerWidth, event.media);
};
When I start the app, the console looks like:
Set up MediaQuery [phone]: (max-width: 512px)
Set up MediaQuery [tablet]: (max-width: 1002px) and (min-width: 512px)
...which looks correct.
However, when I resize the window, this happens (console statements):
onMatchMediaChange [tablet]: 859 (max-width: 1002px) and (min-width: 512px)
onMatchMediaChange [phone]: 436 (max-width: 512px)
onMatchMediaChange [tablet]: 436 (max-width: 1002px) and (min-width: 512px)
The first two logs are correct, but the third is not: for some reason the media query for "tablet" falsely triggers immediately after "phone".
Any pointers what could be wrong?

Is it possible to resize browser window size using javascript? [duplicate]

This question already has answers here:
Can I resize the browser window?
(4 answers)
Closed 6 years ago.
I want to resize my browser window using Javascript to test my responsive webpage design functionality.
But I didn't find any option for the same.
What I found is, we can modify window size for newly pop up window as shown below:
<body>
<p>Open a new window, and resize the width and height to 500px:</p>
<button onclick="openWin()">Create window</button>
<button onclick="resizeWin()">Resize window</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "", "width=100, height=100");
}
function resizeWin() {
myWindow.resizeTo(250, 250);
myWindow.focus();
}
</script>
</body>
I have defined some responsive behaviour using following css code:
#media screen and (max-width: 500px){
//style changes
}
Can we resize browser window where our page is launched? Or it is not possible?
To resize the window, you can simply do this:
window.resizeTo(width, height);
You likely can use resizeBy/resizeTo but why don't you want to use Chrome built-in tester?
You can open developer console cmd/ctrl + alt + I and click on Device Toolbar or cmd/ctrl + shift + M and adjust screen size using predefined templates or create your own
Update
For automation test there are a several ways to adjust it. Which runtime is running your Jasmine tests? For PhantomJS you can use
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = {
width: 480,
height: 800
};
More info here
There are 2 options to implement responsive layout structure :
by using javascript..
function ResponsiveLayout() {
if (screen.width <= 1440) {
$('#content').addClass("pipeline1");
}
else if (screen.width > 1440 && screen.width <= 1600) {
$('#content').addClass("pipeline2");
}
else if (screen.width > 1600) {
$('#content').addClass("pipeline3");
}
};ResponsiveLayout()
By using css3 media query :
#media (min-width: 1200px){css attrabutes}
#media (max-width: 1199px) and (min-width: 981px){css attrabutes}
#media (max-width: 980px) and (min-width: 641px){css attrabutes}
#media (max-width: 640px) and (min-width: 481px){css attrabutes}
#media (max-width: 480px) and (min-width: 321px){css attrabutes}
#media (max-width: 320px){css attrabutes}
Professionally Media query would be better option but can we use javascript where limited access of css file or inline css -:)

remove white-space in isotope js

There is space in-between each item in my grid. http://okbj.ca/ If you click on most recent or resize the window it will remove the space. When you refresh the space comes back. How can I remove this space for good?
I am using the latest versions of chrome, explorer, microsoft edge and firefox. It seems to not work on all of them.
This seems to be a browser-specific issue for you because it appears fine in the latest version of Chrome, Firefox and Safari on OSX.
It appears the issue occurs on Windows. There are two solutions.
Ugly Javascript Hack
Fire a resize event every second. This will force the plugin to recalculate the sizes.
// cross-browser resize event
var crossBrowserResize = function() {
var evt = document.createEvent('HTMLEvents');
evt.initEvent('resize', true, false);
window.dispatchEvent(evt);
};
// fire the event every second
setInterval(function() {
crossBrowserResize();
}, 1000);
Use Media Queries Instead
This type of grid is easily achievable using pure CSS and some media queries. I inspected the elements and they're already using several media queries to adjust how things resize at different breakpoints.
/* 4 columns on tablet landscape and up */
#media screen and (max-width: 1024px) {
.block {
width: 25%;
}
}
/* 2 columns on tablet profile */
#media screen and (max-width: 720px) {
.block {
width: 50%;
}
}
/* 1 column on phone landscape or profile */
#media screen and (max-width: 480px) {
.block {
width: 100%;
}
}
The collapse you want happens only on window resize event (in Chrome). You can dispatch the event once the grid is loaded:
(function fixGrid() {
var img, grid = document.querySelector('.grid');
if (grid) {img = grid.querySelector('img');}
if (img && img.height) {
window.dispatchEvent(new Event('resize'));
grid.style.opacity = 1;
} else {
if (grid) {grid.style.opacity = 0;}
setTimeout(fixGrid, 10);
}
})();

zslider javascript should work only in screen resolution below 768px

i want to use zslider javascript in my website, but can any one tell me how to make it work only in screen resolution below 768px
Here is the zslider script link:
http://www.cssscript.com/demo/mobile-friendly-content-carousel-slider-with-pure-javascript-zslider/
Use media queries to show slider content below 768px:
#media (min-width: 768px) {
.z-slide-wrap {
display: none;
}
}
Than use JavaScript to initialize slider only if width is below 768px:
if (window.innerWidth <= 768) {
var slider1 = new Slider('#demo', '.z-slide-item', {
// OPTIONS HERE
});
}

Change CSS Based on Mobile Orientation

What is the best approach to change a css file when a mobile application page orientation changes from landscape to portrait and vice versa. I need to suport both Android and iPhone only. It seems media queries aren't the cleanest way, any other ideas?
Example
/* For portrait */
#media screen and (orientation: portrait) {
#toolbar {
width: 100%;
}
}
/* For landscape */
#media screen and (orientation: landscape) {
#toolbar {
position: fixed;
width: 2.65em;
height: 100%;
}
p {
margin-left: 2em;
}
}
For more details see here
The below JQuery code seems to work best for me...the binding examples did not.
$(document).ready(function() {
$(window).resize(function() {
alert(window.orientation);
});
});
First give your style sheet include line an id="cssElement" or something.
Then Using jQuery:
$(document).ready(function(){
// The event for orientation change
var onChanged = function() {
// The orientation
var orientation = window.orientation;
if(orientation == 90) {
$('#cssElement').attr('href', '/path/to/landscape.css');
} else {
$('#cssElement').attr('href', '/path/to/portrait.css');
}
};
// Bind the orientation change event and bind onLoad
$(window).bind(orientationEvent, onChanged).bind('load', onChanged);
});
You can use window.onorientationchange event.

Categories