I tried to find something about this on the net, and although it's a minor issue, I feel as if it impacts the site experience. Furthermore, depending on the browser or speed of your computer, you may not notice this problem.
The issue is I have a fixed header on my site. When scrolling up or down, it seems to lag or drag behind... it stays fixed at the top, but while you scroll it jutters and drags at a different pace.
You can see for yourself, here.
It's doing my head in - it seems to only happen on the portfolio page, and not the home page.
I had same issue on Chrome. Solved it by adding
-webkit-backface-visibility: hidden;
to the header element in my stylesheet;
There is good article regarding this.
Hope this helps.
This may be happening because you're trying to do a fair bit on the window scroll event.
Every time a scroll event fires you've got a parallax scroll function that modifies elements with the parallax class (though there don't seem to be any actual elements with that class). You're also checking whether to add the class that changes your header background.
One thing you can do to improve performance is to do as little as possible inside the actual scroll function.
//Menu contrast happens when user scrolls down
// Save 4 jQuery select operations and 2 function calls per scroll event
var myNav = $('nav');
var heightDiff = $('#bg1').height() - myNav.height();
// Use global variables in the scroll function
$(window).scroll(function () {
if (window.pageYOffset < heightDiff)
{
myNav.removeClass('contrast');
}
else
{
myNav.addClass('contrast');
}
});
You can do something similar with the parallax loop as well.
// Save 2 jQuery select operations and one function call per scroll event
var htmlHeight = $('html').height();
var parallaxElems = $('.parallax');
$(window).scroll(function () {
var scrollpercentage = window.pageYOffset/htmlHeight;
var moveoffset = 350*scrollpercentage; // set parallax coeficcient
parallaxElems.css('background-position-y',(50-moveoffset)+'%');
});
Keep in mind that if you use JavaScript to add new elements to the DOM or modify existing elements, you may have to compute your global variables again.
Related
I'm a pure student's beginner, right now I'm trying to create an adaptive menu for my project, but I need to change the color of my background because white on white is a little bit problematic.
What I tried is to create a script in order to add a class 'scroll' to my 'nav' when I'm scrolling down, and removed it when I'm going back to the top.
But as I said I'm a beginner, and it seems I did something wrong with either my script or my CSS.
Can you help me to understand how where I did something wrong?
Thanks for the help !
PS: Sorry for my english I did my best.
`https://codepen.io/Raz7/pen/zYKoJzY`
it's completly messed up, probably due to all the image I put in.
In your script tag you are using a JQuery Selector "$" but you did not add the JQuery library.
To keep things simple I will use the built-in querySelector from the document object and Vanilla Javascript.
The following code will do what you want:
let timeout;
window.addEventListener('scroll', function (e) {
// If there's a timer, cancel it
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Setup the new requestAnimationFrame()
timeout = window.requestAnimationFrame(function () {
// Run our scroll functions
let nav = document.querySelector('nav');
if (document.querySelector('header').getBoundingClientRect().top !== 0) {
nav.classList.add('scroll');
} else {
nav.classList.remove('scroll');
}
});
}, false);
To actually know what the distance to the top is you need a point of reference, in this script I used the header element as a point of reference since the header is relative to the body tag. If the header distance to top is not 0 then add the scroll class to the nav element else remove it. You can see also a timeout and requestAnimationFrame, this helps de-bouncing the scroll event.
Instead of using the JQuery Library, if you are a beginner I suggest learning about Vanilla Javascript and the DOM.
https://www.w3schools.com/js/js_htmldom.asp
In a web page I have an input field and a div that is fixed to the bottom of the window (with these CSS properties: position:fixed; and bottom:0;
I made a Codepen to show what I'm talking about: https://codepen.io/anon/pen/xpQWbb/
Chrome on Android keeps the div visible even when the soft keyboard is open:
However, Safari on iOS seems to draw the soft keyboard over the fixed element:
(I should mention I'm testing on the iOS simulator on my Macbook, because I don't have a working iPhone)
Is there a way to make iOS Safari keep the element visible even when the soft keyboard is open, like how Chrome does it?
I recently ran in to this problem when creating a chat input that should stay fixed at the bottom of the page. Naturally the iOS keyboard displayed on top of the chat input. Knowing the exact keyboard height seems more or less impossible. I embarked on a quest to find a solid value to base my calculations on so i can manually position the chat input container above the keyboard. I wanted to find the actual "innerHeight" value, in other words the currently visible area of the webpage. Due to how the iOS keyboard works, the only way to get that value with the keyboard open seems to be to scroll to the very bottom of the page, and then take a sample of "window.innerHeight".
So, i set up an event listener on my input field on 'click' (since on 'focus' caused a lot of issues for me). This opens the keyboard, which takes a while, so after i set a timeout for 1000ms to make sure (hopefully) that my keyboard is fully open. After 1000ms i quickly scroll to the bottom of the page with javascript, save the value of "window.innerHeight" in this state, and scroll back to where i was. This gives me the actual height of the visible area on the screen.
It seems like the browser window is placed behind the keyboard until you scroll to the very bottom, in which case the whole window 'scrolls up' and the bottom is placed at the top of the keyboard view.
Once i have this value i use currently scrolled value (window.scrollY) plus the value i saved minus the height of my absolute positioned element to determine where to place it. I opted to also hide the input while scrolling since it's flicking around quite a bit. Another downside to this is that you get a quick flick of the page when it does the measurement at the bottom.
Another thing i couldn't solve was the variable height of the address bar. I just made the input a bit higher than i needed so it would have some "padding" at the bottom.
var correctInnerHeight = window.innerHeight;
var isFocused = false;
var docHeight = $(document).height();
var input = $('.myInput');
input.click(function(e){
isFocused = true;
input.css('position', 'absolute');
// Wait for the keyboard to open
setTimeout(function(){
docHeight = $(document).height();
var lastScrollPos = $(document).scrollTop();
// Scroll to the bottom
window.scroll(0, $(document).height());
// Give it a millisecond to get there
setTimeout(function(){
// Save the innerHeight in this state
correctInnerHeight = window.innerHeight;
console.log(correctInnerHeight);
// Now scroll back to where you were, or wish to be.
window.scroll(0, lastScrollPos);
fixInputPosition();
// Make sure the input is focused
input.focus();
}, 1);
}, 1000);
});
input.on('blur', function(){
input.css('position', 'fixed');
input.css('top', 'auto');
input.css('bottom', '');
isFocused = false;
});
$(window).scroll(function(){
fixInputPosition();
});
function fixInputPosition(){
if(isFocused){
var offsetTop = ($(window).scrollTop() + correctInnerHeight) - input.height();
offsetTop = Math.min(docHeight, offsetTop);
input.css('top', offsetTop);
input.css('bottom', 'auto');
}
};
body, html{
margin: 0;
}
html{
width: 100%;
height: 2000px;
}
.myInput{
position: fixed;
height: 30px;
bottom: 0;
width: 100%;
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' class='myInput'>
Check out this thread, it talks about a work around that may be more feasible in terms of code. In brief it talks about using the height of the keyboard to move the content into view. All be it a bit hacky it may be difficult to pin down the exact height of the keyboard across devices.
Unfortunately, due to the nature of the IOs Safari keyboard it's not part of the browser viewport so cannot be referenced as you would do typical elements.
#Bhimbim's answer may a good shot too.
Regards,
-B
i experienced this before. What i did back then was :
Make a listener when keyboard is hit.
When keyboard is hit resize you webview's height with screen height - keyboard height.
To do this trick you need to make sure that you html is responsive.
I can show more code in the IOS side, if you're interested i can edit my answer and show you my IOS code. Thank you.
Hi again, sorry, i was mistaken, i thought you were creating apps with webview inside. If you still wanna do this by listening the keyboard i still have work around for you. It may not the perfect way, but i believe this will work if you want to try. Here my suggestion :
You still can have listener from webpage when the keyboard is up. You can put a listener on your textfield by jquery onkeyup or onfocus.
Then you will know when the input is hit and the keyboard will show.
Then you can create a condition in your java script to manipulate your screen.
Hope this give you an insight friend.
#Beaniie thank you !.
Hi Andreyu !. Yes correct, we can not know the keyboard height, not like my case with WebView, I can know the keyboard height through IOS code. I have another work around, not so smart, but might work. You can get the screen size and compare to array of IOS device screen size. Then you might narrowed down the keyboard height by surveying through IOS devices. Good luck friend.
Try using position:absolute and height:100% for the whole page.
When the system displays the keyboard,it plTaces it on top of the app content.
One way is to manage both the keyboard and objects is to embed them inside a UIScrollView object or one of its subclasses, like UITableView. Note that UITableViewController automatically resizes and repositions its table view when there is inline editing of text fields.
When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following:
1.Get the size of the keyboard.
2.Adjust the bottom content inset of your scroll view by the keyboard height.
3.Scroll the target text field into view.
Check the Apple developer's guideline to learn more:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
So even the Polymer website has this issue. This is an SPA-world problem.
Repro: Go to http://www.polymer-project.org/docs/elements/core-elements.html, click on e.g. core-ajax on the left and scroll down to the middle, then goto core-xhr. Note the scroll position.
What are some best practices considering that I a) want to avoid behavior like the above, but also b) want to preserve the scroll position for when I use the back arrow to goto a page I've already been? It'd be nice if core-pages had support built in.
This might work as a workaround. You can use the fire event in polymer, once something fire the event you listen to the call and force the page to scroll to top. At least this solved my problem with core-animated-pages transition: slide-from-right.
Polymer
this.fire('scroll-top')
Index.html
document.addEventListener('scroll-top', function(){
// Access the main core-header-panel
var scaffold = document.querySelector('core-scaffold');
var scrollArea = scaffold.shadowRoot.querySelector('core-header-panel');
scrollArea.scroller.scrollTop = 0; // Scroll to top
}
)
update 01/01/2018
Polymer now has a nice element for this:
iron-scroll-threshold
I have this complex HTML Layout:
http://jsfiddle.net/5RgjL/2/
As you can see, messages are anchored at the top of the #messages container.
I want it so they are anchored at bottom, so the first message displayed will be at bottom, and not at the top.
Also when resizing the page the view inside the scrolling box must slide up from bottom.
It is hard to explain, i will make you understand with an example:
Populate messages inside the box, till is full and there is the scroll bar.
Try to resize the entire window from bottom, you can see that the message on bottom will be covered. i do not want this, but instead, slide up from bottom.
I tried many things, like to absolute positioning the #message container, but i run into other problems and i cannot get it to work like i want.
I need some help from someone really experienced in HTML/CSS.
If you are familiar with the Facebook messages page, you will understand what i'm trying to do.
PS: Some CSS styles are applied with javascript, because i generate page content dinamically, and only in this page i need those styles.
If I understand correctly then setting an absolute bottom position for the messages until there are more messages than vertical space will solve the first requirement of new messages appearing at the bottom.
This requires a CSS and JavaScript change:
CSS
#messagesWrapper {
position:absolute;
bottom:0;
}
JavaScript
var $messages = $('#messages');
var $messagesWrapper = $('#messagesWrapper');
var $messagePageContent = $('#messagePageContent');
function populateMessages(){
var newMessage = $('<div id="msg-'+i+'" class="aMessage">Another Message<br>Message ID: '+i+'</div>');
i++;
newMessage.hide().appendTo('#messages').stop().fadeIn(400);
if ($messages.height() >= $messagePageContent.height()){
messagesWrapper .css({position:'static'});
$messagePageContent.stop().animate({ scrollTop: $messages.outerHeight() }, 700);
}
}
The scrolling also needs to occur if then window is resized, so attaching a handle to the resize event is also required.
$(window).resize(function() {
$messagePageContent.stop().animate({ scrollTop: $messages.outerHeight() }, 700);
});
However, this is not good enough because the height calculation is missing which resets the CSS position to its default value. This is required for the enscroll jQuery plugin (and I suspect any sort of scrolling) to work.
But we can simply refactor and move all of the code into a common function, for example:
var $messages = $('#messages');
var $messagesWrapper = $('#messagesWrapper');
var $messagePageContent = $('#messagePageContent');
function populateMessages(){
var newMessage = $('<div id="msg-'+i+'" class="aMessage">Another Message<br>Message ID: '+i+'</div>');
i++;
newMessage.hide().appendTo('#messages').stop().fadeIn(400);
scrollMessages();
}
function scrollMessages() {
if ($messages.height() >= $messagePageContent.height()){
$messagesWrapper.css({position:'static'});
$messagePageContent.stop().animate({ scrollTop: $messages.outerHeight() }, 700);
}
}
$(window).resize(scrollMessages);
This is the code I used in this demo. It's probably not the best solution, I was looking at using -webkit-transform: scaleY(-1) to flip the container and then flip the messages back but it messed up the scrolling (i.e. it was backwards!). There might be a better solution using CSS transforms out there.
You can have them added to the bottom by applying this style to your messages id:
position: absolute;
bottom: 0;
Also, if you want the images to add to the top of the list instead of to the bottom you can use prependTo() instead of appendTo().
I'm not sure what exactly you are asking for on the scrolling, so if you could give a bit more info I will update my answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am giving mobile support for a web application. I have a requirement in my application that pull down screen to refresh the page to get the latest updates. I have seen this feature in iPhone native applications and it was also implemented in twitter and foursquare mobile sites.
I have seen some posts here but i unable to understand what exactly they are saying..
I am very new to this environment. please guide me in doing this feature.
Is there any javascript libraries or jqueries are there for this feature?
Essentially, pull to refresh has only been implemented publicly using a hijacked javascript scrolling mechanisms, like iScroll. This is how Twitter is doing it - with some sort of js webkit css3 scrolling library. But, you'll notice even on an iPhone 4, twitter's scrolling in mobile web is janky and not 100% natural.
Yesterday, I wrote a scroll to refresh handler for overflow: scroll components. Now that iPhone is supporting overflow: scroll, we don't have to hijack the scrolling any longer. This will be especially true when Apple fixes the current iOS -webkit-overflow-scrolling: touch bugs.
I can't yet provide my code open source, but here's the interface to do it, with some comments.
(function(window, $, undefined) {
var hasTouch = 'ontouchstart' in window,
startEvent = hasTouch ? 'touchstart' : 'mousedown',
endEvent = hasTouch ? 'touchend' : 'mouseup';
var STATES = {
...
};
var CLASS_NAMES = {
...
};
var PullToReload = function(callback, wrapper, instructionsContent) {
// create all the dom elements and append the right before a content wrapper, but after a primary main wrapper.
// <div class="mainWrapper" style="overflow: scroll; height: 600px;"><div class="pullToReloadWrapper"></div><div class="contentWrapper"></div></div> is the markup.
// Check if the main wrapper's height is bigger than the content wrapper's height. If so, then change the main wrapper height to be the height of the content wrapper.
// scroll main wrapper by the reload wrapper's height.
// set state to pull
// invoke initEvents()
};
PullToReload.prototype.setState = function(state) {
// set the state of either pull, update, or release. Change CSS classes and content.
}
// boiler plate event handling switch
PullToReload.prototype.handleEvent = function(e) {
switch (e.type) {
case startEvent:
this.start(e);
break;
case "scroll":
this.scroll(e);
break;
case endEvent:
this.end(e);
break;
}
};
PullToReload.prototype.initEvents = function() {
// add event listeners for startEvent and endEvent with method "this"
// calling this in an event listener automatically calls handleEvent()
};
PullToReload.prototype.start = function() {
// start listening to on scroll for the wrapper
};
PullToReload.prototype.end = function(e) {
// remove scroll event listener
// if the current state is in release, then set state to update and invoke the callback,
// else the state is in pull, then invoke reset()
};
PullToReload.prototype.scroll = function(e) {
// if current scroll position is almost to the top, change state to release.
// else put it back to pull state.
};
PullToReload.prototype.reset = function() {
// animate scroll to height of reload component.
// put css classes back to the beginning
};
})(window, jQuery, I);
This solution works on iOS5, Safari, Chrome, and probably others. I had to use jQuery in a couple places, mainly animating the scroll.
This solution doesn't require a css3 scroll handler, but just overflow: scroll;
Start by learning JavaScript, XHttpRequests and then touch events.
The pull to refresh is nothing more than a trigger to XHR (AJAX) call that appends new data on the selected element you want.
But if you want a short answer, check Cubiq's iScroll 4 at http://cubiq.org/iscroll-4
Best scrolling JS ever.
Here is the example: http://cubiq.org/dropbox/iscroll4/examples/pull-to-refresh/