changing card data and color on hover, should I set all data to defult state when hover into another card? or just toogle the previous card?

huangapple go评论76阅读模式
英文:

changing card data and color on hover, should I set all data to defult state when hover into another card? or just toogle the previous card?

问题

以下是翻译好的部分:

我需要创建一些卡片组件,用户在悬停在卡片上时,该卡片的数据和颜色将发生变化,并且在用户悬停在另一张卡片之前保持不变。当用户悬停在另一张卡片上时,先前卡片的数据将返回到初始状态,而悬停卡片的数据和颜色将发生变化。

我的问题是,哪种方式是最优化的?

方式1: 当我触发悬停函数时,我将所有卡片的数据还原到初始状态,然后更改悬停卡片的数据。

方式2: 当我触发悬停函数时,我将检查先前更改的是哪张卡片的数据,并将该卡片的数据还原到初始状态,然后更改悬停卡片的数据。

在这两种方式中,哪一种方式更优化?或者是否有其他更优化的方式?

英文:

I have to make some card components, where the user hovers over them. The data and color of this card will change and it will remain unchanged before the user hover over another card, When user hover on another card, data of the previous card will return to the primary state and the hovered card data and color will change.

My question is What will be the optimized way to do it?

**way 1: ** when I hit the hover over function, I will make all card data to the primary state and change the hovered card data.

**way 2: ** when I hit the hover-over function, I will check which card data was previously changed and return that card data to the primary state and change the hover-over card data.

So in those two ways, which way will be better optimized? Or is any other way to do it optimized way?

答案1

得分: 1

这是一种快速简单的方法3:给所有的卡片添加一个类,比如card,并定义一个类,比如hoverState。当你进入一个卡片时,只需从所有卡片元素中移除hoverState,然后将hoverState添加到当前的卡片。

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
function swapImgSrc(elem) {
  let src = elem.dataset.src;
  elem.dataset.src = elem.src;
  elem.src = src;
}

function swapText(elem) {
  let text = elem.dataset.text;
  elem.dataset.text = elem.innerText;
  elem.innerText = text;
}

document.getElementById('container').addEventListener("mouseover", (event) => {
  if (event.target.classList.contains('card')) {
    [...document.getElementsByClassName('hoverState')].map(x => {
      x.classList.remove('hoverState');
      swapImgSrc(x.querySelector('img'));
      swapText(x.querySelector('.swap'));
    });
    event.target.classList.add('hoverState');
    swapImgSrc(event.target.querySelector('img'));
    swapText(event.target.querySelector('.swap'));
  }
}, false);

<!-- 语言: lang-css -->
.card {
  float: left;
  width: 100px;
  border: 1px gray solid;
  padding: 3px 5px; /* 修正:添加了缺失的冒号 */
  margin: 10px 10px;
  text-align: center;
}
.hoverState {
  background-color: lightyellow;
  font-weight: bold;
}

<!-- 语言: lang-html -->
<div id="container">
  <div class="card"><div>Card 1</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 2</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 3</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 4</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 5</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 6</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 7</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 8</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 9</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div class="swap" data-text="Foo changed">Foo bar</div></div>
  <div class="card"><div>Card 10</div><img src="https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text" width="80" data-src="https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text" /><div data-text="Foo changed">Foo bar</div></div>
</div>

<!-- 结束代码段 -->

更新: 我更新了答案,以满足在卡片内部交换图像和文本的新要求。卡片具有以下特点:

  • <img src="..." data-src="..." /> -- img 标签具有 data-src 属性,其中包含悬停时的替代图像URL。
  • <div class="swap" data-text="...">...</div> -- 带有 .swap 类的 div 具有 data-text 属性,其中包含悬停时的替代文本。
  • 在悬停进入/离开时,替代URL/文本与实际URL/文本互换。
英文:

Here is a way 3, which is quick and easy: Add a class to all cards, say card, and define a class, say hoverState. When you enter a card simply remove hoverState from all card elements, then add the hoverState to the current one.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function swapImgSrc(elem) {
let src = elem.dataset.src;
elem.dataset.src = elem.src;
elem.src = src;
}
function swapText(elem) {
let text = elem.dataset.text;
elem.dataset.text = elem.innerText;
elem.innerText = text;
}
document.getElementById(&#39;container&#39;).addEventListener(&quot;mouseover&quot;, (event) =&gt; {
if(event.target.classList.contains(&#39;card&#39;)) {
[...document.getElementsByClassName(&#39;hoverState&#39;)].map(x =&gt; {
x.classList.remove(&#39;hoverState&#39;);
swapImgSrc(x.querySelector(&#39;img&#39;));
swapText(x.querySelector(&#39;.swap&#39;));
});
event.target.classList.add(&#39;hoverState&#39;);
swapImgSrc(event.target.querySelector(&#39;img&#39;));
swapText(event.target.querySelector(&#39;.swap&#39;));
}
}, false);

<!-- language: lang-css -->

.card {
float: left;
width: 100px;
border: 1px gray solid;
padding 3px 5px;
margin: 10px 10px;
text-align: center;
}
.hoverState {
background-color: lightyellow;
font-weight: bold;
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 1&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 2&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 3&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 4&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 5&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 6&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 7&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 8&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 9&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div class=&quot;swap&quot; data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;card&quot;&gt;&lt;div&gt;Card 10&lt;/div&gt;&lt;img src=&quot;https://via.placeholder.com/80x40/c9f7bc.png?text=Original+text&quot; width=&quot;80&quot; data-src=&quot;https://via.placeholder.com/80x40/f7bcbc.png?text=Changed+text&quot; /&gt;&lt;div data-text=&quot;Foo changed&quot;&gt;Foo bar&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

UPDATE: I updated the answer with new requirement to swap image and text within a card. The cards have:

  • &lt;img src=&quot;...&quot; data-src=&quot;...&quot; /&gt; -- the img tag has a data-src attribute that contains the alternative image URL on hover
  • &lt;div class=&quot;swap&quot; data-text=&quot;...&quot;&gt;...&lt;/div&gt; -- the div with .swap has a data-text attribute that contains the alternative text on hover
  • on hover in/out, the alternative URL/text is swapped with the actual URL/text, and vice versa

huangapple
  • 本文由 发表于 2023年1月9日 13:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定