简体   繁体   English

如何通过 Magento2 CMS 中的元素 id 将页面正文滚动到特定位置?

[英]How to scroll the page body to the specific location by element id in Magento2 CMS?

I want to open a product tab on the product page with a click of an anchor tag in Magento2.我想通过单击 Magento2 中的锚标记在产品页面上打开产品选项卡。 The Magento2 CMS version is 2.2.5, below is the code: Magento2 CMS 版本是 2.2.5,下面是代码:

 require([ 'jquery' ], function($) { $(document).ready(function() { $('.mor-link').on('click', function(event) { // Prevent URL change console.log('clicked'); event.preventDefault(); // `this` is the clicked <a> tag $('[data-toggle="switch"][href="' + this.hash + '"]').trigger('click'); }); }); });
 <a id="read_more" class="mor-link" href="#new3.tab">Read More...</a>

. .

So here I want to open the FAQ's product tab with scroll down the page body.所以在这里我想通过向下滚动页面正文来打开常见问题解答的产品选项卡。 You can check on my website as well: https://example.com/staging/tinted-sunscreen.html My script is working fine to open the FAQ's tab but the page body is not scrolling down to the desired element location.您也可以在我的网站上查看: https://example.com/staging/tinted-sunscreen.html我的脚本可以正常打开常见问题解答选项卡,但页面正文没有向下滚动到所需的元素位置。 Please let me know, how I can achieve this thing.请让我知道,我怎样才能做到这一点。 I just want to open FAQ's tab after the click on the 'Readmore...' link and page body jump to that place.我只想在单击“阅读更多...”链接和页面正文跳转到该位置后打开常见问题解答选项卡。

Thanks in Advance, please help.在此先感谢,请帮助。

You can use the scrollIntoView method inside your click handler:您可以在单击处理程序中使用scrollIntoView方法:

$('.mor-link').on('click', function(event) {
  event.preventDefault();
  $('[data-toggle="switch"][href="' + this.hash + '"]').trigger('click');
  document.getElementById("tab-label-new3.tab").scrollIntoView();
});

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

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