单击影响Safari和Chrome浏览器中其他元素悬停区域的项目

问题描述 投票:2回答:1

关于我的应用的某些上下文:

我正在使用简单的create-react-app样板,并有一个按钮列表,每个按钮都有一个关闭的图标和文本。单击关闭图标后,该按钮将从列表中删除。

CSS效果:我在悬停按钮上应用了直通效果。


问题:

在ios Safari / Chrome浏览器中,如果我删除了第一个按钮,悬停效果将添加到第二个按钮。在Android Chrome中不会发生这种情况。

此问题的GIF:enter image description here


与该问题相关的代码:

Component:

export default function App() {
  const [items, updateItem] = React.useState(["apple", "ball", "cat"]);

  const removeItem = itemToBeRemoved => {
    updateItem(currentItems =>
      currentItems.filter(currentItem => !currentItem.includes(itemToBeRemoved))
    );
  };
  return (
    <div className="filters">
      {items.map(item => (
        <a key={item}>
          <span onClick={() => removeItem(item)}>&#x2715;</span>
          <span>{item}</span>
        </a>
      ))}
    </div>
  );
}

样式:

.filters {
  margin: 0 -3px;
  max-width: 100%;
}

a {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  border-radius: 3px;
  border: 1px solid transparent;
  min-height: 30px;
  word-wrap: break-word;
  padding: 5px 12px;
  line-height: 1.2rem;
  background-color: #eee;
  cursor: pointer;
  user-select: none;
  transition: all 0.3s ease;
  margin: 2px 3px;
  padding: 5px 8px;
  font-size: 0.85rem;
}

a:hover,
a:focus {
  background-color: #ccc;
}

a:focus {
  outline: 0;
  border-color: 1px solid blue;
  box-shadow: 0 0 0 2px #0000ff;
}

a span:first-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

a span:last-child {
  display: flex;
  height: 100%;
  top: 0;
  align-items: center;
  border-left: 1px solid gray;
  padding-left: 8px;
  margin-left: 8px;
}

a:hover span:last-child,
a:focus span:last-child {
  text-decoration: line-through;
}

Codesandbox链接:https://codesandbox.io/s/suspicious-swanson-o5jgi


我的理解:

Safari假定指针停留在同一位置,直到下一次点击(在不同位置),即为什么我们在下一个按钮中看到悬停状态。让我知道我是否错。

javascript css reactjs cross-browser
1个回答
0
投票

这可能是由于key属性而发生的,我认为在您的情况下,key应该是数组上属性的索引,而不是对象本身,请尝试一下:)]

© www.soinside.com 2019 - 2024. All rights reserved.