Change background-position horizontal transition for color on scroll jQuery/CSS

spencer

I recently saw this website - https://www.wokine.com/ - where on scroll() the background-position changes from linear gradient with two colors to slide one color off the screen. I learned how they created the split colors with linear-gradient, but I can't figure out how they are doing the animation with the red color 'pushing' the white color horizontally off the screen.

Here is one of the more recent things I've tried. Ultimately, I want to do it on scroll but I was trying it on click for now. I kept the hover style in there for testing.

$(document).ready(function() {
  $("button").click(function() {
    $(".box").css('background-position': 'left');
  });
});
.box {
  width: 100px;
  height: 100px;
  display: inline-block;
  background-size: 200% 200%;
  transition: background-position 1s;
  background-image: linear-gradient(to right, blue 50%, green 0);
  background-position: right;
}

.box:hover {
  background-position: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="box"></div>
<br>
<button>Toggle background position</button>

Bilel

Following the link you shared, I'm using Scroll event and that's your ultimate goal from what I understand.

So we can use $(document).ready() or $(window).on('load',.. to initiate with the first animation using the class .intro.


Then depending on scroll direction we toggle between two classes .slideleft and .slideright. If that's a confusing choice of class names you can call them Up and Down.

$(document).ready(function() {
  $('.box').addClass('intro');
});
(function() {
  var lastScroll = 0;
  $(window).on('scroll', function() {
    if ($(".box").is(".intro")) {
      $('.box').removeClass('intro');
    }
    var activeScroll = $(this).scrollTop();
    //We check the scroll direction
    cond = Boolean(activeScroll > lastScroll);
    //each background sliding effect follow it's scroll direction
    $(".box").toggleClass('slideleft', cond); //DownScroll
    $(".box").toggleClass('slideright', !cond); //UpScroll
    lastScroll = activeScroll;
  });
}());
.box {
  width: 100%;
  height: 200px;
  display: inline-block;
  background-size: 200% 200%;
  transition: background-position 1s;
  background-image: linear-gradient(to right, #ff5851 50%, #F8F8F8 50%);
  background-position: left;
}

.intro {
  background-position: 50%;
}

.slideleft {
  background-position: left center;
}

.slideright {
  background-position: center center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box"></div>
<div class="lorem">
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
  <p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</p>
</div>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Text element background color change

분류에서Dev

Background color change on page load

분류에서Dev

change background color on webpage jQuery

분류에서Dev

Manage scroll position in scrollview during orientation change

분류에서Dev

Update multiple background-position on scroll using jQuery?

분류에서Dev

Change the background color of a layout from the code

분류에서Dev

XPages - change background color of place bar button

분류에서Dev

How to change a font/background color in transparent windows?

분류에서Dev

UITableView change background color of specific cell

분류에서Dev

Change the PS1 color based on the background color?

분류에서Dev

Is it possible to change the X background position and not affect the Y background position of an element in JS or jQuery?

분류에서Dev

Horizontal Scroll Positioning

분류에서Dev

How to change CSS background-position for the X axis only?

분류에서Dev

How to change web browser's tab bar background color or image?

분류에서Dev

knockout-validation change input background color on invalid model

분류에서Dev

Ubuntu SDK QML how to change the background color of TextField

분류에서Dev

Cannot change text and background color of searchbar in navigation bar

분류에서Dev

MFC VC++: CMFCButton::SetFaceColor does not change background(face) color

분류에서Dev

jQuery checkbox select all and change background color on select

분류에서Dev

How to change background-color of selected option in IE11?

분류에서Dev

How do I permanently change the background color of a row in a Listview?

분류에서Dev

hero transition background disappears

분류에서Dev

Horizontal data update is not working on scroll

분류에서Dev

jQuery tabs enable horizontal scroll

분류에서Dev

How to change the document background color when select list option for that color is selected

분류에서Dev

Can you fix a div and make it change opacity, size and position as you scroll with CSS3

분류에서Dev

Change image opacity with transition

분류에서Dev

background-image transition not working?

분류에서Dev

How can I change background color using an if statement in PHP using CSS

Related 관련 기사

  1. 1

    Text element background color change

  2. 2

    Background color change on page load

  3. 3

    change background color on webpage jQuery

  4. 4

    Manage scroll position in scrollview during orientation change

  5. 5

    Update multiple background-position on scroll using jQuery?

  6. 6

    Change the background color of a layout from the code

  7. 7

    XPages - change background color of place bar button

  8. 8

    How to change a font/background color in transparent windows?

  9. 9

    UITableView change background color of specific cell

  10. 10

    Change the PS1 color based on the background color?

  11. 11

    Is it possible to change the X background position and not affect the Y background position of an element in JS or jQuery?

  12. 12

    Horizontal Scroll Positioning

  13. 13

    How to change CSS background-position for the X axis only?

  14. 14

    How to change web browser's tab bar background color or image?

  15. 15

    knockout-validation change input background color on invalid model

  16. 16

    Ubuntu SDK QML how to change the background color of TextField

  17. 17

    Cannot change text and background color of searchbar in navigation bar

  18. 18

    MFC VC++: CMFCButton::SetFaceColor does not change background(face) color

  19. 19

    jQuery checkbox select all and change background color on select

  20. 20

    How to change background-color of selected option in IE11?

  21. 21

    How do I permanently change the background color of a row in a Listview?

  22. 22

    hero transition background disappears

  23. 23

    Horizontal data update is not working on scroll

  24. 24

    jQuery tabs enable horizontal scroll

  25. 25

    How to change the document background color when select list option for that color is selected

  26. 26

    Can you fix a div and make it change opacity, size and position as you scroll with CSS3

  27. 27

    Change image opacity with transition

  28. 28

    background-image transition not working?

  29. 29

    How can I change background color using an if statement in PHP using CSS

뜨겁다태그

보관