css image fixed horizontally absolute vertically - html

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});
}
});
});

Related

Fixed Position DIV is cutoff when scrolling to the bottom

I am working in Angular Material and I am running into an issue when I use property: fixed. The bottom portion of the div that is fixed gets cutoff when I scroll all the way down to the bottom of the page.
here is the JSFiddle: https://jsfiddle.net/baiin/pp1ak0a7/
I've tried other solutions to fix this like using scrollTop() as an alternative to position: fixed, but it also produces the same bug. I think the solution is to check when I scroll if the bottom of the div is hitting the bottom of the page. However, I am unable to get a proper position for the div when it is being moved. I keep getting the same value. I need to find a way to get the position of this div relative to the entire page.
var offset = $("#scroll").offset();
console.log(offset.top); // doesn't give me relative position, just gives me constant value
Any input would help because I've been struggling with this problem for a while.
You can try this, may be it helps you
$(function() {
var getTop = $('#scroll').offset().top;
log(getTop - $(window).scrollTop());
$(window).scroll(function() {
console.log(getTop - $(window).scrollTop());
});
});
Best of luck :)

Fix position of page and move only one div from bottom to top while scrolling

I am developing a parallax website, and in one page(div) I have a some images in another div like
<div> //page which fits in window
<div> //inner div which will be on left side, and this needs to be fixed positionwhile scrolling
Some content....
</div>
<div> //inner div it will be on right side with 4 small images, this div should move from bottom to top while scrolling
<img></img><img></img><img></img><img></img>
</div>
</div>
So to tell shortly, I have a page with two divs, in which I want to move only one div and another should be fixed.
As seen in picture Vision div and background should be fixed and only four images in the right side should move from bottom to top while scrolling.
Once the second div scrolled to top, then I will move the page and show next page.
Any ideas???
Created a fiddle for you here: https://jsfiddle.net/svh8owgz/
In answer to your questions: fixing an image regardless of scrolling can be done with the CSS with position: fixed.
So in my example - I created the div with class companyInfo and styled it:
.companyInfo {
position:fixed;
top:0%;
left:2%;
width: 200px;
height:200px;
background-color: blue;
}
Which would be the part where it says 'Vision' in your image.
Then as for the thumbnailcontainer - that's just got a relative positioning within the overall container:
div.imageContainer {
position:relative;
left: 400px;
width: 200px;
top:400px;
}
That's pretty straight forward: I will just move as expected ( at least - if the page container is larger than the screen height and scrolling is possible to begin with ).
Then you mentioned 'Once the second div scrolled to top, then I will move the page and show next page'
What you could do is use the jQuery.appear plugin to check whether elements come into visible range or not. What I did was add a div with id pageTrigger below the actual page container. That means you can start listening to events of users scrolling to the bottom of you page:
(function ($) {
$('#pageTrigger').appear();
$('#pageTrigger').on('appear', function (event, $all_appeared_elements) {
// Trigger page change
alert("page change triggered");
});
})(jQuery);
Be sure you reference jQuery and jQuery.appear in your page otherwise this will not work. Also - I couldn't find a nice HTTPS CDN for this plugin so I had to include the source - sorry for that.

Make div fixed to top when scroll past 100% height

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

IE8 Issue with fixed and absolute position

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();
});
});

Div fixed on vertical scroll but absolute on horizontal

here's what I want. I want a top bar AND navigation menu attached to the bar. I've done this no problem. I want to bar and the navigation menu to follow me if I scroll up and down.I can do this with a position fixed. but, if i have a fixed position, then when i scroll left to right, they follow.
I want to have both the top bar and the navigation menu follow as the user scrolls up and down but if they scroll left to right i want it to act like an absolute position, and become partially or completely hidden (depending on how much the user scrolls).
Is this possible? I've seen a couple of topics but haven't been able to get it to work for me.
Here is my jfiddle http://jsfiddle.net/kyleseitz/rX4Vh/11/
I want EVERYTHING to move down with me when I scroll down and back up when I scroll up. But, if i get a horizontal scroll bar, I want to pass the viewing window over it.
I found the javascript on a previous question but i can't get it to work for me.
.slim {position: absolute;}
<div id="phantombar" class="slim">
<!--I Technically don't need these if they are not neccessary-->
<div id="phantombar" class="fixed_elem">
<div id="headWrap">
ScrollToFixed is a jQuery plugin that used to fix elements on the page (top, bottom, anywhere); however, it still allows the element to continue to move left or right with the horizontal scroll.
Website: https://github.com/bigspotteddog/ScrollToFixed
Demo: http://bigspotteddog.github.io/ScrollToFixed/
try to use this. Maybe works :
$(function(){
var elements = $('#ptm, #spt, #support, #act, #left_nav');
elements.css('position', 'absolute');
$(window).scroll(function(){
if($(this).scrollLeft()){
elements.removeAttr('style').css('position', 'absolute');
}else{
elements.removeAttr('style').css('position', 'fixed');
}
});
});
elements can you add more, depending on needs.