Skip Navigation

[Resolved] HOw to create a sticky compare button and Post Title

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to have some of their text to be sticky so that it scrolls with the user when they scroll on the browser page.

Solution:

You can use the follow to do this.

HTML

<div id="sticky-anchor"></div>
<div class="sticky"><h5>[wpv-post-link]</h5></div>

CSS

.stick {
  position: fixed;
  top: 0;
  z-index: 10000;
}

Javascript

function sticky_relocate() {
  var window_top = jQuery(window).scrollTop();
  var div_top = jQuery('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    jQuery('.sticky').addClass('stick');
  } else {
    jQuery('.sticky').removeClass('stick');
  }
}
 
jQuery(function() {
  jQuery(window).scroll(sticky_relocate);
  sticky_relocate();
});

So what this does is that it detects the browser scroll and then applies the css to stick the items to the top of the page.

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by alishiaM 5 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1371491
Screenshot 2019-10-28 at 14.18.57.png
Screenshot 2019-10-28 at 14.18.47.png
Screenshot 2019-10-28 at 15.01.51.png
Screenshot 2019-10-28 at 15.01.31.png

Tell us what you are trying to do?
Hi Toolset team. I have a job website created with a custom post type and displayed using Views with search filters. I add the Compare function and so far so good. I would just like to know if it is possible to create my Compare button and Post title on the results page with a sticky position? I tried some code from w3schools but it isn't working.
Thank you, Alishia

Is there any documentation that you are following?
hidden link
Is there a similar example that we can see?
hidden link
What is the link to your site?
I have trying this on a test site: hidden link

#1371541

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alishia,

Thank you for getting in touch.

Could you try adding top:0; to the css as well as I see this is missing from your code.

Thanks,
Shane

#1371555

Hi Shane, thank you for your quick response.

I do have the top:0; in my CSS. Currently have the following:
In the Loop editor: <div class="sticky"><input id="compare" type="submit" value="Compare"></div>

In the CSS:
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}

Still not working.
Thank you for your help

#1371637

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alishia,

Would you mind allowing me to have admin access to the site to help resolve this for you.

The private fields are enabled for your next response.

Thanks,
Shane

#1372409

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alishia,

It should now be working.

I Used these codes

HTML

<div id="sticky-anchor"></div>
<div class="sticky"><h5>[wpv-post-link]</h5></div>

CSS


.stick {
  position: fixed;
  top: 0;
  z-index: 10000;
}

Javascript

function sticky_relocate() {
  var window_top = jQuery(window).scrollTop();
  var div_top = jQuery('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    jQuery('.sticky').addClass('stick');
  } else {
    jQuery('.sticky').removeClass('stick');
  }
}

jQuery(function() {
  jQuery(window).scroll(sticky_relocate);
  sticky_relocate();
});

So what this does is that it detects the browser scroll and then applies the css to stick the items to the top of the page.

Please let me know if this helps.
Thanks,
Shane

#1372541

Shane I can not thank you enough! This is perfect. I am doing my happy dance 🙂 Thank you so much. I have learned something new 🙂

#1372543

My issue is resolved now. Thank you!