1

I'm developing a custom shopping cart functionality where multiple items are able to be bought on a single page, and when an add to cart button is pressed, the page refreshes, but moves to the top of the document.

Sometimes the action throws up errors or notices so i'd like to be able to keep the current scroll height when the page is refreshed

I tried adding javascript but cant seem to get it to work-- here's what im using, the first line is the function that adds any messages or notices. Are there anyways to accomlish this using strictly php? or is there a reason my function isnt working?

<?php wc_print_notices(); ?>
<div id="messageScroll"></div>
<script>
    $(window).load(function() {
    $("html, body").animate({scrollTop:$("#messageScroll").offset().top}, 800);
});
</script>
<?php
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
Gazow
  • 1,019
  • 2
  • 11
  • 16
  • You're using a form that has the action set to the same page you are on, is that correct? – Chris Trudeau Jan 03 '16 at 21:49
  • 0_0 - you realize you are using javascript to do this - correct? What you should do is apply an anchor in the position you want and when the page is refreshed include the anchor or use javascript to go to the anchor. – Ryan Rentfro Jan 03 '16 at 22:39
  • Chris, yes it is set to the same page. ryan- i was using javascrip because i couldnt find a way to do it in php after searching for a few hours – Gazow Jan 04 '16 at 04:24

1 Answers1

1

It sounds like you're submitting a form on the button click with the action attribute either not set, or set to the page you are on. You can use this to scroll by setting action="#messageScroll"

I've actually answered a similar question before: Contact Form Thank You Top of Page

I'm just guessing about your setup, so if this solution doesn't work for you let me know and I'll re-evaluate the situation.

Community
  • 1
  • 1
Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
  • this seems to work well thanks! i assumed it was using the action attribute for something, but it didnt appear to have one. The only issue i have is that it puts the element at the very top of the scroll position which cuts some stuff off, i dont suppose theres a way to subtract a pixel ammount from the position – Gazow Jan 04 '16 at 04:37
  • No problem, and yes, check out this question: http://stackoverflow.com/questions/17534661/make-anchor-link-go-some-pixels-above-where-its-linked-to There are a variety of different solutions to choose from – Chris Trudeau Jan 04 '16 at 04:40