JavaScript Why does replaceWith() not trigger the same way as css()?

I'm trying to trigger a change in the content of a fixed div when you scroll past the top of an image.

$(window).scroll(function() {
    var y_scroll_pos = window.pageYOffset;

    var img_position = $('img').position();

    if(y_scroll_pos > img_position.top) {
        $(".fixed").replaceWith(
            "<p>Goodbye!</p>"
        );
    }

    else {
        $(".fixed").replaceWith(
            "<p>Hello!</p>"
        );
    }

[...]

This only works if your window is scrolled to that point or below on load, even though window.pageYOffset is, obviously, continuously changing as you scroll.

But this example, with css(), changes as it should.

if(y_scroll_pos > img_position.top) {
        $(".fixed").css(
            "background-color","red";
        );
    }

    else {
        $(".fixed").css(
            "background-color","yellow";
        );
    }

[...]

Why? What's the difference between these two methods?

Answer:1

I am currently working on an application and I need the back button to go back multiple pages. I am familiar with the history.back function as shown below. //go back to the last view $rootScope....

I am currently working on an application and I need the back button to go back multiple pages. I am familiar with the history.back function as shown below. //go back to the last view $rootScope....

  1. back button back button
  2. back button back home center apk
  3. back button download back button download
  4. back button app back button app

I've spent quite a while trying to connect Socket.IO-Client to a server running on Sails.js framework. The client is basically a simple JavaScript application running with Node.js on Raspberry Pi. ...

I've spent quite a while trying to connect Socket.IO-Client to a server running on Sails.js framework. The client is basically a simple JavaScript application running with Node.js on Raspberry Pi. ...

  1. connect socket client
  2. connect socket client to server
  3. connect socket client c++
  4. connect client socket programming
  5. connect socket io client
  6. java connect socket client
  7. socket connect tutorial
  8. python socket connect client
  9. android socket connect client
  10. network socket connect client
  11. websocket client connect
  12. socket client connect to room
  13. socket client connect timeout
  14. socket client connect example
  15. connect to socket.io client side

I'm using Gulp to run Webpack because some things that are trivial with Gulp are messy with Webpack, such as multiple outputs. However, babel-loader doesn't seem to be doing anything. When I have JSX ...

I'm using Gulp to run Webpack because some things that are trivial with Gulp are messy with Webpack, such as multiple outputs. However, babel-loader doesn't seem to be doing anything. When I have JSX ...

  1. gulp using webpack
  2. using gulp and webpack together

Here is the code: $q.all($q.when(3), $q.when(5)).then(function (values) { console.log(values); }); The output is the following: {"$$state":{"status":1,"value":3}} The manual states that: ...

Here is the code: $q.all($q.when(3), $q.when(5)).then(function (values) { console.log(values); }); The output is the following: {"$$state":{"status":1,"value":3}} The manual states that: ...