Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 如何使用Gutenberg应用平滑滚动_Wordpress - Fatal编程技术网

Wordpress 如何使用Gutenberg应用平滑滚动

Wordpress 如何使用Gutenberg应用平滑滚动,wordpress,Wordpress,我有一个菜单,它有如下链接 https://examples.com/#about-us https://examples.com/#our-team https://examples.com/#testimony https://examples.com/#career https://examples.com/#contact-us 我有一个单页应用程序,并希望应用平滑滚动那里 我在谷歌上搜索了一下: generate\u smooth\u scroll\u元素允许您启动平滑 在其他元素类

我有一个菜单,它有如下链接

https://examples.com/#about-us
https://examples.com/#our-team
https://examples.com/#testimony
https://examples.com/#career
https://examples.com/#contact-us
我有一个单页应用程序,并希望应用平滑滚动那里

我在谷歌上搜索了一下:

generate\u smooth\u scroll\u元素允许您启动平滑
在其他元素类上滚动,而不是仅使用
平滑滚动

使用以下PHP代码段将平滑滚动应用于所有哈希 链接:

我只想平滑滚动到特定的类


如何使用gutenberg块编辑器应用特定类并平滑滚动。

在这里,尝试一下这个插件。但是,您应该通过如下方式向平滑滚动链接添加“平滑滚动”类来指定平滑滚动链接:


const smooth_links=document.querySelectorAll('a.smooth_scroll[href^=“#“]”);
用于(平滑链接的常量链接){
link.addEventListener(“单击”,clickHandler);
}
函数clickHandler(事件){
event.preventDefault();
const href=this.getAttribute(“href”);
document.querySelector(href).scrollIntoView({
行为:“平稳”
});
}

你问的是Gutenberg(基于js的),并提供了一个PHP示例),这是行不通的。可以在Javascript的帮助下完成,很多问题都是这样的:我实际上是wordpress的新手,所以不确定什么可以或不能完成。如果我需要html、css或javascript,那也没关系。如果你需要html、js、css或php方面的帮助,你对Wordpress并不陌生,你只是开发人员。我相信youtube上有很多有用的东西。看看这篇文章,例如
data type=“internal”data id=“#test”
的意义是什么?我刚刚从古腾堡复制了这部分内容,供参考。这就是它向元素添加锚定的方式。
add_filter( 'generate_smooth_scroll_elements', function( $elements ) {
  $elements[] = 'a[href*="#"]';
  
  return $elements;
} );
<?php
/**
 * Plugin Name: Native Smooth Scroll
 * Plugin URI:
 * Description: Simple plugin for smooth scroll to anchors using native JS scrollIntoView() method.
 * Author: Paul Fedorov
 * Author URI: https://github.com/acerus
 * Requires at least: 5.0
 * Tested up to: 5.4
 * Version: 1.0
 * Stable tag: 1.0
 *
 * Text Domain: native-smooth-scroll
 * Domain Path: /languages/
 *
 */

function enqueue_scripts() { ?>

  <script>

  const smooth_links = document.querySelectorAll('a.smooth_scroll[href^="#"]');

  for (const link of smooth_links) {
    link.addEventListener("click", clickHandler);
  }

  function clickHandler (event) {
    event.preventDefault();
    const href = this.getAttribute("href");

    document.querySelector(href).scrollIntoView({
      behavior: "smooth"
    });
  }

  </script>

<?php }

add_action('wp_footer', 'enqueue_scripts');