4

Here, i am trying to set image as fix position & move the content to its right scrollable using Jquery , as of now its not working well. Is their any way to make it work ?

2 Answers2

3

As you commented, jQuery is not mandatory, so here is the CSS solution, no need of jQuery

position: sticky is your friend here.

check the updated codePen here.

or check the snippet below.

.phase1_main {width: 100%;display: inline-block;position:relative;}
img {width: auto;max-width: 100%;height: auto;}
#sticky {padding: 0.5ex;}
#sticky.stick {position: fixed;top: 0;z-index: 10000;}
.phase1_inner {position: absolute;top: 0;z-index: 1;width: 8px;height: 100%;background-color: #f1f1f1;}
.progress-container {height: 100%;width: 8px;background: #ccc;}
.progress-bar {width: 8px;background: #4caf50;height: 0%;}
.content {padding: 100px 0;margin: 50px auto 0 auto;width: 80%;}
.phase1_lgt, .phase1_rgt {float: left;width: 50%;}
.phase1_rgt {float: right;position: relative;}
.img42 {width: 100%;display: inline-block;}
.as.active {opacity: 1;}
.as {font-size: 50px;line-height: normal;padding: 50px;opacity: 0.2;}
.phase1_lgt {position: sticky; top:0;}
<div class="phase1_main">
  <div class="phase1_lgt">
    <div id="sticky-anchor"></div>
    <div id="sticky">
      <img class="img-fluida" src="https://cdn.shopify.com/s/files/1/0054/3978/3001/files/lady_hair1_600x.jpg">
    </div>  
  </div>
  <div class="phase1_rgt">
    <h2>Healthy hair begins at the scalp</h2>
    <p>We believe wellness takes time, and it applies to your scalp and hair.</p>  
    <div class="phase1_inner">
      <div class="progress-container">
        <div class="progress-bar" id="myBar"></div>
      </div>  
    </div>
    <div class="tab_content">
      <div class="as">
        <small>Phase 1</small>
        <span>Removal</span>
        <p>+ Sebum and build up removal</p>
        <p>+ Itchiness starts to lessen</p>
      </div>
      <div class="as">
        <small>Phase 2</small>
        <span>Nourishment</span>
        <p>+ Scalp inflammation reduced</p>
        <p>+ Deeper absorption of nutrients</p>
        <p>+ Hair adjusts to absence of silicones, some flatness may occur</p>
      </div>
      <div class="as">
        <small>Phase 3</small>
        <span>Adaption</span>
        <p>+ Hair adjusts to essential oils and natural ingredients</p>
        <p>+ Hair gains additional shine from natural sources, no longer silicones</p>
        <p>+ Scalp inflammation and flakiness has significantly reduced</p>
      </div>
    </div>
  </div>
</div>
Atul Rajput
  • 4,073
  • 2
  • 12
  • 24
  • can you share the change ? –  Jul 30 '20 at 11:35
  • what do you mean @HamendraSunthwal, I have shared the codepen link above and code also is in the snippet? I didn't get you my friend. – Atul Rajput Jul 30 '20 at 11:39
  • That comment was sent before thanks for the answer –  Jul 30 '20 at 11:40
  • But i want that picture to get non fixed once this section on the right side is completed to scroll –  Jul 30 '20 at 11:40
  • Glad I helped, if this really useful then don't forget to mark it the right answer. – Atul Rajput Jul 30 '20 at 11:41
  • That is also Done, position sticky work the way you want, just try to scroll all the way up, it will non fixed – Atul Rajput Jul 30 '20 at 11:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218890/discussion-between-hamendra-sunthwal-and-atul-rajput). –  Jul 30 '20 at 11:43
  • I am not sure as "fixed" style is already there in the template so i don't want to change that. –  Jul 30 '20 at 15:52
  • I appreciate your effort for answering the question. –  Jul 30 '20 at 15:53
1

Use position: fixed; on your img in the css

https://jsfiddle.net/9ovxs2Lm/

You could use position: sticky as well, but if you only want the photo to be in one place, you should use position: fixed, as "sticky" is intended to toggle between relative and fixed. And fixed, stays fixed. Judging by your question, this is what you want.

Read answers here: Difference between position:sticky and position:fixed?

EDIT: OP wanted it to change value position after reaching the bottom of the text on the right.

Check this fiddle: https://jsfiddle.net/4va5doc0/1/

I've used jQuery to set the height of the div which contains all your text, and used it to change the position-value to absolute after you scroll an amount equal to the text-content height. It will then place the image on the exact place where the height of your text-content, and I subtracted the height of the image so it aligns with the bottom part of the image, and not the top. I added another div in the bottom of your HTML so you could test this. I believe this answers your question?

Here is the jQuery added:

$(function() {
  //caches a jQuery object containing the header element
  var img = $(".img-fluida");
  $(window).scroll(function() {
    var scroll = $(window).scrollTop();
    var height = $(".tab_content").height();
    if (scroll >= height - 526) {
      img.css("top", height - "526");
      img.css("position", "absolute");
    } else {
      img.css("position", "fixed");
      img.css("top", "");
    }
  });
});
R. Srour
  • 158
  • 11
  • Let me check, the codepen –  Jul 30 '20 at 11:38
  • Go ahead bro, it works fine here :) – R. Srour Jul 30 '20 at 11:40
  • 1
    @HamendraSunthwal Btw, you should use this solution, with position:fixed, instead of sticky, as sticky is intended to toggle between position: fixed and position:relative. And judging by your question, you only want the image to be in a fixed position. – R. Srour Jul 30 '20 at 11:42
  • Yes seems a good answer to question –  Jul 30 '20 at 11:52
  • I have a question based on this –  Jul 30 '20 at 11:53
  • @R.Srour just read the thread in comments on my answer please, @Hamendra mentioned it: `But i want that picture to get non fixed once this section on the right side is completed to scroll` – Atul Rajput Jul 30 '20 at 12:06
  • Well you should include that in your question. This is when you use position sticky. And place both sections into one div of a given height. It will stop when the section is done scrolling I assume. Or else you could adf jquery/js to it, so it stops after a given amount of scroll. – R. Srour Jul 30 '20 at 12:09
  • Check the new fiddle, I edited the answer, it's working as you described now. Good? :) @HamendraSunthwal – R. Srour Jul 30 '20 at 14:14
  • Yes this seems to be working, i want to the image to be sticky untill the right side text gets completely scrolled or scrolled down and when i scroll up it stays again in the same place as defined style. –  Jul 30 '20 at 15:50
  • I have used "inherit" in postion style, not fixed not sticky –  Jul 30 '20 at 15:52
  • I appreciate your effort for answering the question. –  Jul 30 '20 at 15:53
  • Look at the jquery. I set position in there. It overrides the css. Please select as correct answer as this solved your issue. Thanks. – R. Srour Jul 30 '20 at 16:22