英文:
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('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);
<!-- 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 -->
<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>
<!-- end snippet -->
UPDATE: I updated the answer with new requirement to swap image and text within a card. The cards have:
<img src="..." data-src="..." />
-- theimg
tag has adata-src
attribute that contains the alternative image URL on hover<div class="swap" data-text="...">...</div>
-- thediv
with.swap
has adata-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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论