JavaScript Fix/stick image to top after scrolling - the image jumps to the top straight away?

I have an image in the center of my webpage. Once the user scrolls and the image hits the top of the window, I am attempting to fix the image to the top of the page so that it scrolls with the page.

However, instead of it sticking when it hits the top of the window, the image is currently jumping to the top of the page as soon as the user scrolls. Although I would like the image to stay fixed at the top while the user is on the page, it stays fixed when the page is refreshed!

Can't seem to figure this out - please help!

index.html

<img src="./assets/img/logo.png" class="logo" alt="Logo">

style.css

.logo {
   width: 500px;
   display: block;
   position: absolute;
   top: 200px;
   left: 0;
   right: 0;
   margin: auto;
} 

main.js

$(function() {
  var boxInitialTop = $('.logo').offset().top;
  $(window).scroll(function () {
    if ($(window).scrollTop() > boxInitialTop) {
      $('.logo').css({position: 'fixed', top: '0px'});
    } else {
      $('.logo').css({position: 'absolute'});
    }
  });
});
Answer:1

your code works.

you only have to reset the top value in the else section. This way it stays 0px because it was set this way in the if section.

Answer:2

You need to put the original placement of the element back to where it started (after the else). Once the element returns to position:absolute you need to ensure that it's top property is returned back to 200px otherwise it will still hold the value that was set when it was fixed i.e. 0.

See below:

$(function() {
  var boxInitialTop = $('.logo').offset().top;
  $(window).scroll(function () {
    if ($(window).scrollTop() > boxInitialTop) {
      $('.logo').css({position: 'fixed', top: '0px'});
    } else {
      $('.logo').css({position: 'absolute', top : boxInitialTop+'px'});
    }
  });
});
Answer:3

I want to set date and time to 2016-05-11T00:00:00.000Z var date = new Date(2016,4,12,0,0,0,0); but instead of showing Date 2016-05-11T00:00:00.000Z its giving Date 2016-05-11T19:00:00.000Z I want ...

I want to set date and time to 2016-05-11T00:00:00.000Z var date = new Date(2016,4,12,0,0,0,0); but instead of showing Date 2016-05-11T00:00:00.000Z its giving Date 2016-05-11T19:00:00.000Z I want ...

  1. javascript setting date
  2. javascript setting date format
  3. javascript date setting time zone
  4. javascript date setting time
  5. javascript datepicker setting date

I need to add CSS properties on each element of a collection with jQuery. Any suggestions? var jsonCss = { "color": "#555555", "font-weight": "bold", "text-align": "left" }; var $...

I need to add CSS properties on each element of a collection with jQuery. Any suggestions? var jsonCss = { "color": "#555555", "font-weight": "bold", "text-align": "left" }; var $...

  1. properties collection in java
  2. properties collection in spring
  3. properties collection in c#
  4. properties in collection
  5. properties in collection javatpoint
  6. collection properties in blue prism

I am using the hammer.js touch library to run certain actions on text within an svg file. A sample can be seen at http://codepen.io/cmgdesignstudios/pen/mebqeW In my sample, if you tap any letter, ...

I am using the hammer.js touch library to run certain actions on text within an svg file. A sample can be seen at http://codepen.io/cmgdesignstudios/pen/mebqeW In my sample, if you tap any letter, ...

I need to filter JSON result using jQuery grep.My JSON result look like this: var data = { "items":[ { "Name": "Name 1", "City": "city1" }, { "Name": ...

I need to filter JSON result using jQuery grep.My JSON result look like this: var data = { "items":[ { "Name": "Name 1", "City": "city1" }, { "Name": ...

  1. filtering json object
  2. filtering json object in javascript
  3. filtering json object angularjs
  4. filtering json object angular
  5. filter json object by value
  6. filter json object by key
  7. filter json object by value javascript
  8. filter json object typescript
  9. filter json object python
  10. filter json object in node js
  11. filter json object c#
  12. filter json object array in javascript
  13. filter json object based on key
  14. filter json object array
  15. filter json object by value java
  16. filter json object java
  17. filter json object by property
  18. filter json object array jquery
  19. filter json object js
  20. filter json object jquery