简体   繁体   English

如何阻止按钮向下滚动到联系表单?

[英]How do I stop button from scrolling down to Contact form?

I'm not a coder or developer.我不是编码员或开发人员。 I'm trying to save us money, and fix a problem with my wife's responsive WordPress site.我正在努力为我们省钱,并解决我妻子的响应式 WordPress 网站的问题。

We hired an SEO guy who said he could also write custom JavaScript for us.我们聘请了一位 SEO 人员,他说他也可以为我们编写自定义 JavaScript。 He created a call to action button that behaves differently between desktop (scroll to Contact Us form) and mobile (Call Now).他创建了一个号召性用语按钮,该按钮在桌面(滚动到“联系我们”表单)和移动设备(立即致电)之间的行为不同。

I noticed he was duplicating the function, in the header custom HTML, to scroll to form on both function calls of the button.我注意到他正在复制标题自定义 HTML 中的函数,以便在按钮的两个函数调用上滚动到表单。 I changed his custom code in the header to use href="tel:xxxxxxxxxx" .我在标题中更改了他的自定义代码以使用href="tel:xxxxxxxxxx"

It still scrolls to the contact form.它仍然滚动到联系表格。 I used the same HTML for our contact info in the footer, and that works fine.我在页脚中为我们的联系信息使用了相同的 HTML,并且效果很好。 I'm stumped.我难住了。

Please help.请帮忙。

Here is the code he wrote:这是他写的代码:

JavaScript: JavaScript:

setTimeout(function() {

  window.scrollTo(0, 0);
}, 1);

jQuery(function($){

  function scrollToForm() {
    $([document.documentElement, document.body]).animate({
          scrollTop: $('#contact-form').offset().top
      }, 1000);
  }

  if (window.location.hash == '#contact-form'){
    setTimeout(function(){
        scrollToForm();
    }, 1000);
  }

  $('.top-cta-btn').click(function(e){   
    scrollToForm();

    if (window.location.href == 'https://www.sironatherapies.com/#contact-form' || 
'https://www.sironatherapies.com/') {
      return false;
    }
  });

});

Custom HTML Header:自定义 HTML 标题:

<style>
    .top-cta-btn {
        background: #dd9f27;
    padding: 13px;
    color: #000;
        cursor: pointer;
    }
    .top-cta-btn:hover {
        color: #fff;
    }

    @media (min-width: 320px) and (max-width: 767px) {

        .header_bottom_right_widget_holder {display: block !important}
        .top-cta-container {display: none}
        .top-cta-container-mobile {display: block;
    margin-bottom: 30px;
    margin-top: 35px;}
        .testimonials-section {display: none}

        .side_menu_button {display: none;}
        .vc_custom_1454330137581 {display: none;}
    }
    @media only screen and (max-width: 1000px){
        .q_logo a {
            left: -80%;
            width: auto!important;
        }
    }
    @media only screen and (min-width: 768px) {
        .top-cta-container {display: block}
        .top-cta-container-mobile {display: none}
    }

</style>
<div class="top-cta-container">
    <a class="top-cta-btn" href="/#contact-form">Book an Appointment Today</a>
</div>

<div class="top-cta-container-mobile">
    <a class="top-cta-btn" href="tel:xxxxxxxxxx">Call Now</a>
</div>

You need to change the CSS selector in the Call-to-Action event handler, in order to target only the Desktop CTA button and not the mobile one:您需要更改 Call-to-Action 事件处理程序中的 CSS 选择器,以便仅针对桌面 CTA 按钮而不是移动按钮:

Now:现在:

  $('.top-cta-btn').click(function(e){   
    scrollToForm();
    ...

Should be:应该:

  $('.top-cta-container .top-cta-btn').click(function(e){   
    scrollToForm();
    ...

This way, you are limiting the scrolling, only when the element .top-cta-btn that is inside the .top-cta-container element (Desktop/Contact Form link) is clicked.这样,您就限制了滚动,仅当单击.top-cta-container元素(桌面/联系表单链接)内的元素.top-cta-btn

The mobile CTA button will not get triggered since it is inside another element, the .top-cta-container-mobile .移动 CTA 按钮不会被触发,因为它位于另一个元素.top-cta-container-mobile


Also, there's no need for the duplicate code.此外,不需要重复的代码。 You can remove it.你可以删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM