I have a page with much content. When I open it I have a vertical scroll bar.
I have a small div that should stick to the right side and centered (vertically).
And when user scroll it should be in the same position never move just to be there in the center all the time.
And this work in chrome,ff but I have issues with ie8.
Chrome: div stick as it should be vertical center in the viewport of the browser.
IE8: div stick to the right but it use vertical center of the whole content and I need just a viewport. So if content is height=4000px in ie8 it centers it at 2000th pixel so user have to scroll to see it.
What I did wrong here?
position:fixed;
_position:absolute;
top: 50%;
right: 0;
You could do this with javascript. Here's an example, using jQuery, that I would try:
var calculatedTop = $(window).innerHeight() / 2;
$('#YourDiv').css('top', calculatedTop);
Edit: I've expanded this a bit to recalculate the top position when the user scrolls. Hopefully this helps with your IE8 problem.
$(document).ready(function(){
var reposition = function() {
var calculatedTop = $(window).innerHeight() / 2;
calculatedTop += $(document).scrollTop();
$('#YourDiv').css('top', calculatedTop);
}
// call reposition immediately on initial load
reposition();
// attach to scroll event
$(window).on('scroll', function(){
reposition();
});
});
Related
I have a windows store app with a column of scrollable text in the center. On the top and bottom I want to have fixed widgets which do not move when scrolling.
I've gotten this to work just fine using some quite simple html
However, when the soft keyboard / touch keyboard is displayed, the bottom of the window is hidden (I would have expected it to resize) and the content is out of view until scrolled to the end. I can see how this probably works quite well for some apps, but for mine it's a disaster. bottom widgets are occluded by the keyboard and top ones scroll out of view when I scroll the center text column to the end.
Here's an imgur gallery of screenshots that show what I mean. I gave up trying to screencapture this after two hours.
Here's the source of my demo app
https://dl.dropboxusercontent.com/u/568631/ninjaScroll2.zip
I can detect when the keyboard is shown or hidden, but I can't seem to do anything about it. I can't resize the window (window.height cannot be set). I can move my bottom widgets to just above the keyboard position, but the window will still scroll when it reaches it's nadir, and then the top widgets are gone.
Does anyone have a workaround for this issue? Is there some way to control the actual window height, or stop this weird viewport scroll effect?
Once the content window has scrolled to it's maximum of 100% (hidden beneath the keyboard) the window itself then starts to scroll up, hiding the top left / top center / top right divs
I couldn't reproduce this scenario. By my side when scrolling to 100%, the touch-keyboard also cover the bottom of the window, and the window didn't start to scroll itself. I'm targeting SDK 14393.
I can move my bottom widgets to just above the keyboard position, but the window will still scroll when it reaches it's nadir, and then the top widgets are gone.
You are targeting the right direction. When the touch-keyboard is on, the window scroller is shown, so that you can scroll to the see the bottom when the touch-keyboard is on. Thus when you scroll the content to the bottom and continuing scrolling, the window will be 'pulled' to up.
So, the workaround is to adjust the position of the bottom divs in window.onscrolling:
default.js:
var windowHeight = window.innerHeight;
var inputPane = Windows.UI.ViewManagement.InputPane.getForCurrentView();
...
window.onscroll = function (evt) {
//change the position of bottom div
var myDiv = document.getElementById("myDiv");
myDiv.style.top = window.pageYOffset+"px";
}
inputPane.onshowing = function (eventArgs) {
document.getElementById("myDiv").style.height = windowHeight-eventArgs.occludedRect.height+"px";
}
inputPane.onhiding = function (eventArgs) {
document.getElementById("myDiv").style.height = windowHeight + "px";
}
default.html:
<div id="myDiv" style="position:absolute;height:100%;width:100%;">
<div style="position:absolute;top:2vh;left:2vw">top left</div>
<div style="position:absolute;top:2vh;right:50vw">top center</div>
<div style="position:absolute;right:2vw">top right</div>
<div style="position:absolute;top:50vh;left:2vw">middle left</div>
<div style="position:absolute;top:50vh;right:50vw">middle center</div>
<div style="position:absolute;top:50vh;right:2vw">middle right</div>
<div style="position:absolute;bottom:2vh;left:2vw">bottom left</div>
<div style="position:absolute;bottom:2vh;right:50vw">bottom center</div>
<div style="position:absolute;bottom:2vh;right:2vw">bottom right</div>
</div>
Notes: To simplify the problem, I use a div to wrap the fixed content. And adjust the position by changing the height of the div.
And here is the link to complete demo: ninjaScroll2.
I've been looking all over on how to fix a div when scrolling past 100% and then for it to sit again when below 100%.
I have been using this jsfiddle to work when i want to sit at a certain pixel height.
Any help would be greatly appreciated.
Alternatively, having the div fix when another div is on visible may work just as well.
Thanks in advance.
Here is the code for jsfiddle.
function fixDiv() {
var $cache = $('#getFixed');
if ($(window).scrollTop() > 100)
$cache.css({'position': 'fixed', 'top': '10px'});
else
$cache.css({'position': 'relative', 'top': 'auto'});
}
$(window).scroll(fixDiv);
fixDiv();
just position the menu at the top of the screen with fixed positioning :
#myMenu{
position : fixed;
top : 10px;
left : 10px;
}
From your comments I think this is what you are trying to achieve. The way this will act is :
You have a <div> with 100% height, you click a button and the page scrolls down to the next div which is 100% height. Then you cna click to go to the next or the last <div>. Meanwhile the menu is always staying right at the same spot. This is what Fixed positioning means. no matter where you scroll that menu div is always going to stay in the
same spot.
Edit
try this if you want the menu to hide while scrolling , then appear again in the same spot.
$(document).scroll(function(){
$('#myMenu').hide();
});
then you would have to show again , you can find a solution with exact code that works , but that is an idea
I've got a page design that uses CSS keyframes to make a min-height: 100% <section> element containing page content slide in from the right. Once the element has animated to its final position, it is possible to "drag-scroll" the page horizontally by selecting and dragging the text, which I want to avoid.
Both the <html> and <body> elements are set to overflow-x: hidden;, which works for preventing scrollbars but still allows scrolling functionality. I do need users to be able to scroll the <section> element vertically.
I'm assuming the problem is caused by the initial translateX value in the keyframe animation, but I'm not sure how to prevent the horizontal drag scrolling.
Here's a jsfiddle demonstrating the problem.
I appreciate any input.
Perhaps a javascript solution? How is the following:
function bindEvent(e, eventName, callback) {
if(e.addEventListener) // new browsers
e.addEventListener(eventName, callback, false);
else if(e.attachEvent) // IE
e.attachEvent('on'+ eventName, callback);
};
bindEvent(document.body, 'scroll', function(e) {
document.body.scrollLeft = 0;
});
I currently have an image in a div that's set to fixed, and I like how its behaving, however I want it also to behave as if it's absolute when scrolling up and down. So in other words, I want fixed behavior horizontally, but absolute vertically (so that I add content to it and can scroll down and see the whole image).
Hopefully, I'm clear. Heres my html:
<div id="graphpaper">
<img src="Background2.jpg" />
</div>
My css:
#graphpaper{
position:fixed;
left:50%;
margin-left:-800px;
width:1600px;
height:1600px;
z-index:-10;
}
This is not an easy topic. The only way to achieve it is to use javascript. You have to 2 ways to do this.
Use position fixed and hook to windows scroll event and change the vertical position with javascript on scroll
Use position absolute (so the image scrolls with the window) and again with javascript on horizontal scroll change the horizontal location.
A basic example (it won't work, but this is what you need to start with):
$(window).scroll(function(){
$('#fixedImage').css('top', $(window).scrollTop());
});
Thanks for your help. I found the solution: This essentially will move the fixed div up (notice the negation sign in front of scrollVar) which will resemble an absolute scrollable behavoir.
$(document).ready(function() {
$(window).scroll(function() {
var scrollVal = $(this).scrollTop();
if ( scrollVal >= 0) {
$('#graphpaper').css({'position':'fixed','top' :-scrollVal});
}
});
});
I want to create a menu bar that stays vertically in place when you scroll up and down the screen, but if the user scrolls left and right, i want the menu bar to move left and right with the rest of the content.
For example, if the user is on the home page and decides to scroll down to read the announcements. I would want the left menu bar to stay in place. but lets say the user zooms up on our webpage so the entire page doesnt fit on the screen. If the user decides to scroll all the way to the right side of the page, i would want the menu bar to disappear along with the other content on the very left of the page.
Is there a way to do this with only css?
I had a similar problem and I found some help and did some customizations for my own need. Check my code below:
CSS
#sidebar {
top:70px; // make it for your own
left: 0px; //make it for your own
position: absolute;
}
jQuery
var leftOffset = parseInt($("#sidebar").css('left'));
$(window).scroll(function() {
$("sidebar").css({
'left': $(this).scrollLeft + leftOffset
})
});