I've been searching through the web for a solution that can viabilize box-sizing in the IE7 (I know, the year is 2014, but the project (therefore the clients) still demand this). I really need box-sizing because the entire project is responsive.
I found this boxsizing.htc and it works pretty well in almost any case. But, unfortunately, it is not my case.
Because I'm using angularjs and I have a div with three columns (children divs with float left) inside and i can click on a button to change the number of columns. When this happens the boxsizing.htc process all its calculations again making my width and height smaller each time I change the number of columns.
So, I thought, angularjs can solve this. I found this pretty interesting link
I could adapt that code into this:
myApp.directive('resizable', function($window){
return {
restrict: 'A',
link: function(scope, element){
scope.initializeWindowSize = function(){
var win = angular.element($window);
scope.windowHeight = win.innerHeight();
scope.windowWidth = win.innerWidth();
scope.sideHeight = scope.windowHeight - 80;
/* ie 7 */
var workArea = angular.element('.workArea');
var workWidthPadding = parseInt(workArea.css('paddingLeft')) + parseInt(workArea.css('paddingRight')) + parseInt(workArea.css('borderRightWidth')) + parseInt(workArea.css('borderLeftWidth'));
var workHeightPadding = parseInt(workArea.css('paddingTop')) + parseInt(workArea.css('paddingBottom')) + parseInt(workArea.css('borderTopWidth')) + parseInt(workArea.css('borderBottomWidth'));
scope.workWidth = scope.windowWidth - 228 - 210 - workWidthPadding;
scope.workHeight = scope.sideHeight - 60 - 25 - 2 - workHeightPadding;
console.log('height', scope.workHeight);
};
scope.initializeWindowSize();
angular.element($window).bind('resize',function(){
scope.initializeWindowSize();
scope.$apply();
});
}
}
});
But that code don't solve all of my problems, just when I can relate the div's width or height with window width or height which it is not all the cases.
Finally, I decide, let's make a jQuery plugin. I came up with this:
$.fn.boxsizing = function(){
// if(navigator.appVersion.indexOf("MSIE 7.") != -1){
var thisWidth = this.width(),
thisHeight = this.height(),
thisborderLeft = parseInt(this.css('borderLeftWidth')),
thisborderTop = parseInt(this.css('borderTopWidth')),
thisborderRight = parseInt(this.css('borderRightWidth')),
thisborderBottom = parseInt(this.css('borderBottomWidth')),
thispaddingLeft = parseInt(this.css('paddingLeft')),
thispaddingTop = parseInt(this.css('paddingTop')),
thispaddingRight = parseInt(this.css('paddingRight')),
thispaddingBottom = parseInt(this.css('paddingBottom')),
newWidth = thisWidth - thisborderLeft - thisborderRight - thispaddingLeft - thispaddingRight,
newHeight = thisHeight - thisborderTop - thisborderBottom - thispaddingTop - thispaddingBottom;
console.log(newWidth, newHeight);
this.css({'width':newWidth, 'height':newHeight});
// }
}
This almost worked.
I can't figured how to apply this code in window resize.
$(window).resize(function(){
$('div.FOO').boxresizing();
});
Can someone help? I don't know if angularjs can handle alone this kind of stuff or, if I really need this plugin and, how can I make things work in the window resize.
Thank you and sorry for my poor english.
Related
I'm trying to add a simple counter in the bottom of my app like this one:
And it is very simple atm, 80 is my array.length that is being populated through my axios request.
<div>{people.length.toLocaleString()}</div>
And as I scroll down the page, using react-infinite-scroll, the number goes up and up and this is just fine. What I'm trying to do is subtract the number as the user goes back up the page.
Is this something harder than I'm thinking? If so, don't give me the full answer, just give me the path to follow. Thanks.
This is what I'm trying to accomplish: https://mkorostoff.github.io/hundred-thousand-faces/
you can do by using scroll event with window.innerHeight and the element bottom height to check whether its available inside the display window.
You can try like this using onscroll event which is available in library itself.
let counter = 0;
[listofElement].find(ele => {
var conditionHeight = window.innerHeight;
var cordinat = ele.getBoundingClientRect().top;
counter++;
return conditionHeight < cordinat;
});
You can check here with sample working part.
Looking at the source of the page you've linked, the code uses this function to get the size of the page:
function getScrollPercent() {
var face_width = document.getElementById('first').clientWidth;
var face_height = document.getElementById('first').clientHeight;
var body = document.documentElement || document.body;
var faces_per_row = Math.floor(main.clientWidth / face_width);
var total_height = total / faces_per_row * face_height;
var scroll_percent = (body.scrollTop - main.offsetTop + body.clientHeight) / total_height;
var count = Math.floor(scroll_percent * total);
var chunked_count = count - (count % faces_per_row);
if (chunked_count > 0) {
counter.classList = "fixed";
}
else {
counter.classList = "";
}
return (chunked_count > 0) ? chunked_count : 0;
}
The essential bit is var scroll_percent = (body.scrollTop - main.offsetTop + body.clientHeight) / total_height;. Basically, if you can calculate your total height (assuming that isn't infinite), then you can use body.clientHeight, +/- offsets, divided by totalHeight to figure out how far down the page you are. Call this from an event listener on scroll, and you should be good to go.
Incidentally, if this is the infinite scroll library you're talking about using, it's no longer maintained in favor of react-infinite-scroller.
using react-infinite-scroll, you can't back axios request results or remove generated doms.
The solution is calculating width and height of every doms and calculate offset.
Check how many doms are above the scrollReact and so so.
$myWindow.on('resize', function(){
var $width = $myWindow.width();
if ($width > 870) {
console.log('hey im 870');
$('#the-team-wrapper .flex-content').empty();
ajax_results.done(function(data) {
// console.log(data.job_titles[3]);
var employee_job_titles;
function job_titles_display(jobtitle,ind){
if (jobtitle.job_titles[ind].length>1) {
var my_array = [];
for (var i = 0; i < jobtitle.job_titles[ind].length; i++){
my_array.push(jobtitle.job_titles[ind][i][0]['title']);
employee_job_titles = my_array.join(' | ');
}
}else {
var employee_job_titles;
employee_job_titles = jobtitle.job_titles[ind][0]['title'];
}
return employee_job_titles;
}
for (var i = 0; i < data.employee_info.length; i++) {
if(i%2 == 0){
$('#the-team-wrapper .flex-content').append('<div class="profile-parent"><div class="employee-profile-pic flex-item" data-id="'+data.employee_info[i]['id']+'"></div><div class="employee-bio-wrapper flex-item"><h2 data-id="'+data.employee_info[i]['id']+'">'+data.employee_info[i]['firstname']+" "+data.employee_info[i]['lastname']+'</h2><h3 data-id="'+data.employee_info[i]['id']+'">'+job_titles_display(data,i)+
'</h3><p class="employee-bio-text employee-bio-text-not-active">'+data.employee_info[i]['bio']+'</p></div><button type="button" class="bio-prev-butt-left">View '+data.employee_info[i]['firstname']+'\'s'+' Bio</button><div class="hide-bio-close-button-left">x</div></div>');
}else {
$('#the-team-wrapper .flex-content').append('<div class="profile-parent"><div class="employee-bio-wrapper flex-item"><h2 data-id="'+data.employee_info[i]['id']+'">'+data.employee_info[i]['firstname']+" "+data.employee_info[i]['lastname']+'</h2><h3 data-id="'+data.employee_info[i]['id']+'">'+job_titles_display(data,i)+'</h3 data-id="'+data.employee_info[i]['id']+
'"><p class="employee-bio-text employee-bio-text-not-active">'+data.employee_info[i]['bio']+'</p></div><div class="employee-profile-pic flex-item" data-id="'+data.employee_info[i]['id']+'"></div><button type="button" class="bio-prev-butt-right">View '+data.employee_info[i]['firstname']+'\'s'+' Bio</button><div class="hide-bio-close-button-right">x</div></div>');
}
var profile_pic_path = data.employee_info[i]['profile_pic'].split('\\').join('\\\\');
$("#the-team-wrapper .flex-content-wrapper .flex-content .employee-profile-pic:eq("+i+")").css({'background': 'url(_employee_pics/'+profile_pic_path+')','background-repeat': 'no-repeat','background-position': 'center', 'background-size': 'cover'});
}
});
}
I have this code, and it should fire when width is greater than 870, but instead it fires when width is greater than 970 on Opera, and when width is about 890 on Chrome. How can I fix this and get consistent results across browsers. Thanks in advance.
Are you using a CSS reset to neutralize the browser's default margin or padding on the <body> element?
Different browsers add different amounts of either padding or margin to the <body> of the page, which could explain why the function is triggered at different points in different browsers.
The problem is, the resize event fires at different times and rates depending on browser, CPU load, and how fast you actually do the resizing.
Test the following code in your browsers. When I do this in a clean browser at a reasonable rate of coverage the difference usually comes in within around 2px of the target.
(BTW, you'll see I am caching the jQuery selectors into variables. Not strictly necessary for this test, but you might be surprised to find out how many bugs I've fixed because coders have invoked uncached jQuery selectors willy-nilly in loops and other repetitive places throughout their code).
var $window = $(window);
$window.on('resize',function(){
var w = $window.width();
if (w > 1000) {
console.log( w );
} else {
console.log('nope: ' + w)
}
});
I'm trying to optimise my website (http://www.mazion.co.uk).
As such, I tried to create critical CSS for the site using penthouse. (See Critical CSS used here - this was generated by the main developer of penthouse for me).
However, when using critical CSS, one of the subpages on my website does not load properly. BUT, when I fully inline the CSS (or don't do anything to optimise CSS), this sub-page loads correctly.
On this sub-page - http://www.mazion.co.uk/courses, there are a number of boxes that are resized using a JS function (see below) that is run on.ready and on.resize (i.e. when resizing the screen) which ensures that all boxes are of the same size.
When using critical CSS, the resizing function works on.resize but not on.ready. On the other hand, with inline CSS, the resizing function works as expected on.resize and on on.ready...
Thus, I was wondering if someone could help me in identifying the problem. I have tried to inline the styles for the boxes directly into the HTML, but I was unsuccessful...
You can see this problem by going to http://www.mazion.co.uk/courses/ and having a look at the boxes. If you then resize your browser, all the boxes will resize themselves so that they are all the same height... This resizing that make all the boxes the same height should actually happen automatically when the page loads....
Js Function (Not Extremely important to question, but helps in setting the scene)
jQuery(document).ready(function($){
$(window).resize(function() {
resizeCourseBoxes()
resizeTopBespokeCoursesBoxes()
resizeMidBespokeCoursesBoxes()
}).resize(); // Trigger resize handlers.
});
// Ensure that all the courses boxes are the same height (this ensures that the rows are of the same size...)
function resizeCourseBoxes() {
jQuery(function($) {
courseHeader = $('.course_header')
maxTextHeight = Math.max.apply(
Math, courseHeader.map(function() {
return $(this).height()
}).get())
for (var i = 0; i < courseHeader.length; i++) {
currentHeight = courseHeader[i].offsetHeight
new_padding = Number(maxTextHeight) - currentHeight + 10
courseHeader[i].style.marginBottom = new_padding + 'px'
};
})
}
// Ensure that all mid section (prices section) of the bespoke section is the same
function resizeTopBespokeCoursesBoxes() {
jQuery(function($) {
CoursePriceSection = $('.green_bx_top')
maxTextHeight = Math.max.apply(
Math, CoursePriceSection.map(function() {
return $(this).height()
}).get())
for (var i = 0; i < CoursePriceSection.length; i++) {
currentHeight = CoursePriceSection[i].offsetHeight
new_padding = Number(maxTextHeight) - currentHeight + 10
CoursePriceSection[i].style.marginBottom = new_padding + 'px'
};
})
}
// Ensure that all mid section (prices section) of the bespoke section is the same
function resizeMidBespokeCoursesBoxes() {
jQuery(function($) {
CoursePriceSection = $('.green_bx_mid')
maxTextHeight = Math.max.apply(
Math, CoursePriceSection.map(function() {
return $(this).height()
}).get())
for (var i = 0; i < CoursePriceSection.length; i++) {
currentHeight = CoursePriceSection[i].offsetHeight
new_padding = Number(maxTextHeight) - currentHeight
CoursePriceSection[i].style.marginBottom = new_padding + 'px'
};
})
}
The answer to my problem was simple:
Critical CSS is specific for each HTML page. Thus critical CSS for each individual page should be calculated separately...
(I was using the same critical CSS for all my subpages...).
I have a somewhat weird problem. I created a chrome extension that will automatically scroll down as users edit a google document. It seems to be working okay when the doc is in paginated mode. If I switch it to compact mode, then it seems like jQuery won't get all the requested elements. The basic HTML tree is pretty much the same in paginated and compact modes. I'm stumped and not sure why it's not working correctly. We should be looking mostly at how '$currentPage' works. Sometimes the height returns 0 when it shouldn't happen.
[EDIT] Here's when it'll mess up: It works fine in paginated mode. But when I start the extension in compact mode when there are text already on page 2, it'll work as expected. If I delete texts all the way back to page 1, it'll still work as expected. But if I start typing text back into page 2, it'll start to mess up.
Here's the code:
function autoScrollFunc(){
if(autoscrollEnabled){
var oldCount = pageCount;
// get all the pages
var $pages = $("div.kix-page");
// count how many pages there are
pageCount = $pages.length;
console.dir($pages);
console.dir($pages.last());
console.dir($pages.last().find("div.kix-paragraphrenderer"));
$currentPage = $pages.last().find("div.kix-paragraphrenderer").first();
//console.dir($currentPage);
//pageHeight = $pages.first().height();
if(pageCount != oldCount){
// update data if there are changes in page count
console.log("Number of pages changed! Count: " + pageCount);
pageHeight = $pages.first().height();
}
// scroll to new position
scrollToNewPosition();
}
}
function scrollToNewPosition(){
if(autoscrollEnabled){
oldHeight = viewLoc;
var upperSection = toolbarHeight + getRulerHeight();
var windowHeight = $(window).height();
var marginHeight = (pageCount > 1) ? getTopMarginHeight() : firstMarginHeight;
var docViewHeight = (windowHeight > upperSection) ? (windowHeight - upperSection) : 0;
// calculate the scroll location
var totalDocHeight = (pageHeight * (pageCount-1)) + $currentPage.height() + marginHeight;
viewLoc = (totalDocHeight < docViewHeight) ? 0 : totalDocHeight - docViewHeight + $currentPage.height();
console.log("upperSection: %s, windowHeight: %s, marginHeight: %s, docViewHeight: %s, totalDocHeight: %s, viewLoc: %s, currentPage.height(): %s",
upperSection, windowHeight, marginHeight, docViewHeight, totalDocHeight, viewLoc, $currentPage.height());
if(oldHeight != viewLoc){
// if there are changes in height, then scroll to the new position
$docScroller.animate({ scrollTop: viewLoc}, 250);
}
}
}
If you want to try the extension yourself, here's the project URL:
http://code.google.com/p/google-docs-autoscroll/
Thanks in advance, guys.
I'm trying to get the total height of a page using JavaScript so I can check if the page is long enough to display something, however in my testing I am unable to get the total height of a page.
I've looked around on the Internet but things like this don't seem to be well documented, as all I can find is scrollHeight, which, as I might mention, doesn't work.
Any way to use JavaScript to find it?
Without a framework:
var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
document.documentElement.scrollHeight is working ok for me in the latest version of Internet Explorer, Chrome, Firefox and Safari.
Have you tried $(document).height(); ?
Demo here
Height of entire page...
document.body.offsetHeight
Height of viewport...
var h,
de = document.documentElement;
if (self.innerHeight) {h = window.innerHeight;}
else if (de && de.clientHeight) {h = de.clientHeight;}
else if (document.body) {h = document.body.clientHeight;}
This article may help you.
To find the height of the entire document you could just find the highest DOM Node on current page with a simple recursion:
;(function() {
var pageHeight = 0;
function findHighestNode(nodesList) {
for (var i = nodesList.length - 1; i >= 0; i--) {
if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
pageHeight = Math.max(elHeight, pageHeight);
}
if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
}
}
findHighestNode(document.documentElement.childNodes);
// The entire page height is found
console.log('Page height is', pageHeight);
})();
NOTE: It is working with Iframes.
Enjoy!
Perhaps use the position of an element at the bottom of the page, like:
$("#footer").offset().top
Have a look at the documentation. One of the supported methods: height, innerHeight, outerHeight may be suitable for you.