我在svelteKit中遇到了碰撞问题.该按钮打开菜单,但当我试图关闭它时,它没有打开.当我在按钮外单击时,这会触发handleClickOutside调用clickOutside.js,然后关闭菜单.

我认为问题是element.contains包括按钮,因此菜单没有关闭.

然而,我无法修复它.

我用的是tailwindUItailwindCSSSvelteKit.

100

<script>
    // @ts-nocheck
    import bostonLogo from '../../img/bostonLogo.png';
    import { clickOutside } from '../../lib/clickOutside';

    // Example Profile
    let profile = {
        name: 'Matias',
        lastName: 'Barletta'
    };

    // Show/Hide Menu

    let menu = false;

    // COLLISION WITH HANDLENAV
    
    function handleClickOutside(event) {            
        menu = false;   
    }

    function handleNav() {

        menu = !menu;
        
    }

</script>

<div>
    <!-- Static sidebar for desktop -->
    <div class=" md:flex md:w-64 md:flex-col md:fixed md:inset-y-0" class:hidden={!menu}>
        <!-- Sidebar component, swap this element with another sidebar if you like -->
        <div
            use:clickOutside
            on:click_outside={menu? handleClickOutside : ''}
            class:absolute={menu}
            class:mt-11={menu}
            class="md:flex-1 md:flex md:flex-col md:min-h-0 bg-gray-800"
        >
...

100

// @ts-nocheck
/** Dispatch event on click outside of element */
// @ts-ignore

export function clickOutside(element) {
    // @ts-ignore

    const handleClick = (event) => {
        console.log(event.target, document.body)
    
        // element exist?, element contain where i did click, preventDefault = false?
        if (element && !element.contains(event.target) && !event.defaultPrevented) {
            element.dispatchEvent(
                // Dispatch and create new custom event.
                new CustomEvent('click_outside', element)
            );
        }
    };
    // add eventlistener when you click on document
    document.addEventListener('click', handleClick, true);

    return {
        destroy() {
            document.removeEventListener('click', handleClick, true);
        }
    };
}

推荐答案

不久前,我也遇到过同样的问题.我的解决方案是在菜单打开时忽略菜单点击.你已经有了这个:on:click_outside={menu? handleClickOutside : ''}.

所以现在如果菜单关闭,动作就不会启动.如果菜单打开,菜单按钮将不会启动.所以总是只有一个回调被调用.

function handleNav() {
    if (menu) return;
    menu = !menu;
}

作为替代方案,您可以向操作添加选项,以便如果目标位于被忽略的 Select 器列表中(而不仅仅是它所在的元素),则handleClick函数可以忽略单击.

使现代化

这里的替代方法是working REPL.

我们要做的是在菜单按钮上添加一个ID

<button
  id="menu-button"
  type="button"
  value="button"
  class="-ml-0.5 -mt-0.5 h-12 w-12 inline-flex items-center justify-center rounded-md text-gray-500 hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500"
  on:click={handleNav}
  >...</button>

然后,我们将该ID作为参数添加到use:clickOutside操作中:

<div
  use:clickOutside={{ignore: 'menu-button'}}
  on:click_outside={menu? handleClickOutside : ''}
  class:absolute={menu}
  class:mt-11={menu}
  class="md:flex-1 md:flex md:flex-col md:min-h-0 bg-gray-800"
  >
  <div class="h-40 w-40 bg-slate-600 p-3 text-white">
    Some menu content here
  </div>
</div>

然后在动作中,我们通过ID获取元素,并判断我们是否在其中.

const handleClick = (event) => {
  event.preventDefault();
  
  // This function kinda rearranged with early returns to take the new parameter into account.
  if (!element) return;
  if (element.contains(event.target)) return;
  
  //Get the element based on the id we gave in the params
  const ignore = document.getElementById(opts.ignore)
  //Check that we're not clicking in the button.
  if (ignore.contains(event.target)) return;
  
  element.dispatchEvent(
    // Dispatch and create new custom event.
    new CustomEvent('click_outside', element)
  );
};

Javascript相关问答推荐

获取表格的左滚动位置

是否有方法在OpenWeatherMap API中获取过go 的降水数据?

为什么从liveWire info js代码传递数组我出现错误?

Vega中的模运算符

使用续集和下拉栏显示模型属性

如何将react—flanet map添加到remixjs应用程序

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

单个HTML中的多个HTML文件

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

为什么这个.add.group({})在教程中运行得很好,但在我的游戏中就不行了?

让chart.js饼图中的一个切片变厚?

变量在导入到Vite中的另一个js文件时成为常量.

将异步回调转换为异步生成器模式

面对代码中的错误作为前端与后端的集成

触发异步函数后不能显示数据

AJAX POST在控制器中返回空(ASP.NET MVC)

当S点击按钮时,我如何才能改变它的样式?

无法向甜甜圈图表上的ChartJSImage添加可见标签

带有JS模块模式的Rails的Importmap错误:";Net::ERR_ABORTED 404(未找到)&Quot;