将鼠标悬停在文本上时,更改文本和背景图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将鼠标悬停在文本上时,更改文本和背景图像相关的知识,希望对你有一定的参考价值。

我刚开始使用javascript进行编码和处理,这对我来说仍然很困难。

我的问题是,我有一个导航菜单,其中只有一个单词。我希望当我将鼠标悬停在整个站点的导航菜单上时,其背景会相应于我所悬停的导航菜单项而发生更改,并且该导航项会将其内容从一个单词更改为更多单词。

我曾尝试在Google,YouTube和此处的stackoverflow上进行搜索。我设法获得了可以运行的代码,但是感觉像是一团糟并且是一种不好的做法,我无法为我的代码实现良好的答案,或者他们使用的ES5 JavaScript与ES6有所不同。

这是我尝试过的事情:

1。我尝试的第一件事是用if语句创建一个函数。没工作。可能是由于我对JS的了解不足。

  1. 然后我尝试将EventListener分别添加到每个导航菜单项。那行得通,但这是一团糟。仅复制/粘贴代码块。

  2. 然后我尝试再次创建一个函数,并且在html内包含onmouseover =“ function()”,但也无法正常工作。

  3. 然后,我尝试通过CSS进行操作,但是我的经验不足再次表明了这一点。我无法通过:before和:after来成功实现。

我在这里有一些示例代码。

<div id='bckgimg'>

<div class="nav-container">
  <a href='/nav1' id='nav-nav1' class='nav'>NAV1</a>
  <a href='/nav2' id='nav-nav2' class='nav'>NAV2</a>
  <a href='/nav3' id='nav-nav3' class='nav'>NAV3</a>
  <a href='/nav4' id='nav-nav4' class='nav'>NAV4</a>
</div>
</div>

<style>

#bckgimg
    background-image: url('https://picsum.photos/id/1/300/200');


</style>

<script>

const nav1 = document.getElementById('nav-nav1');
const nav2 = document.getElementById('nav-nav2');
const nav3 = document.getElementById('nav-nav3');
const nav4 = document.getElementById('nav-nav4');
const bckgImg = document.getElementById("bckgimg");

// repeat this code 3 more times with nav2, nav3, nav4
// code bellow checks if <div> is hovered over and changes background image <div> + changes text of the <div>

navStories.addEventListener('mouseenter', e => 
  bckgImg.style.backgroundImage = "url('https://picsum.photos/id/2/300/200')";
  navStories.innerHTML = "MY NAV 1 LONGER";
);

// if <div> is hovered out it returns the original picture to background <div> and original text of <div>

navStories.addEventListener('mouseleave', e => 
  bckgImg.style.backgroundImage = "url('https://picsum.photos/id/1/300/200')";
  navStories.innerHTML = "NAV1";
);

</script>
答案

好了!一整天都花了我,但我明白了!

首先,在JSfiddle上显示JavaScript + No-Library(pure JS),您需要按侧面的箭头,对于LOAD TYPE选择no wrap-body bottom

第二,如果您的代码中的错误由JSfiddle用红色表示,则整个目录(在我的情况下为JavaScript目录)将无法运行。我的代码中的错误是我只是在/ *处注释了我不需要的所有代码,但由于我认为它并不重要而没有关闭它。

第三,解决方案本身:

// changed whole thing, which was my point. Reduced code from ~50 lines to two functions in 16 lines

// onHover function has a property of i which corresponds to numbers in html on onmouseover and out

// created arrays with IDs, what text and image should be after hovering
// i will correspond to numbers in html which in turn correspond to numbers in arrays

function onHover(i) 
  const array = ["nav-nav1", "nav-nav2", "nav-nav3", "nav-nav4"];
  const strings = ["MY NAV 1 LONGER", "MY NAV 2 LONGER", "MY NAV 3 LONGER", "MY NAV 4 LONGER"];
  const bckgUrls = ["url('https://picsum.photos/id/2/300/200", "url('https://picsum.photos/id/3/300/200')", "url('https://picsum.photos/id/4/300/200')", "url('https://picsum.photos/id/5/300/200')"];
  var idvar = document.getElementById(array[i]);
  idvar.innerHTML = `$strings[i]`;
	document.getElementById("bckgimg").style.backgroundImage = `$bckgUrls[i]`;
 

// when hovering out from text do same like onHover just the reverse
// return values of navigation menu and background image to original

function outHover(i) 
  const array = ["nav-nav1", "nav-nav2", "nav-nav3", "nav-nav4"];
  const strings = ["NAV1", "NAV2", "NAV3", "NAV4"];
  var idvar = document.getElementById(array[i]);
  idvar.innerHTML = `$strings[i]`;
	document.getElementById("bckgimg").style.backgroundImage = "url('https://picsum.photos/id/1/300/200')";
 
#bckgimg
    background-image: url('https://picsum.photos/id/1/300/200');
    overflow: hidden;
    background-position: center center;
    background-size: cover;
    height: 300px;


a 
  color: white;
  text-decoration: none;
<div id='bckgimg'>

<!-- because we changed ON LOAD options now onmouseover and on mouseout will pull functions from JS -->

<div class="nav-container">
  <a onmouseover="onHover(0)" onmouseout="outHover(0)" href='/nav1' id='nav-nav1' class='nav'>NAV1</a><br>
  <a onmouseover="onHover(1)" onmouseout="outHover(1)" href='/nav2' id='nav-nav2' class='nav'>NAV2</a><br>
  <a onmouseover="onHover(2)" onmouseout="outHover(2)" href='/nav3' id='nav-nav3' class='nav'>NAV3</a><br>
  <a onmouseover="onHover(3)" onmouseout="outHover(3)" href='/nav4' id='nav-nav4' class='nav'>NAV4</a><br>
</div>
</div>

以上是关于将鼠标悬停在文本上时,更改文本和背景图像的主要内容,如果未能解决你的问题,请参考以下文章

使用css将鼠标悬停在按钮上时如何更改站点背景图像?

悬停时如何更改导航栏链接上的文本颜色(和背景)? [复制]

将鼠标悬停在 li 上时如何更改所有字体颜色

悬停在列表项上时更改背景图像

将鼠标悬停在特定 <li> 上时如何更改 <ul> 背景图像? [复制]

将鼠标悬停在图像链接上时显示文本