简体   繁体   English

当您 hover 将鼠标悬停在文本上时,我应该更改文本颜色、大小和字体。 我怎样才能使这种情况持续发生?

[英]i am supposed to change the text color, size, and font when you hover the mouse over the text. How can i made this happen continiuously?

the mouseon Function changes the text to green, and its position. i also need it to change font and size. mouseon Function 将文本更改为绿色,其 position。我还需要它来更改字体和大小。 i need these two functions to happen all the time when the mouse is hovered over the text.当鼠标悬停在文本上时,我需要这两个功能一直发生。 i want the mouse off function to bring the text back to normal.我希望鼠标关闭 function 以使文本恢复正常。

function mouseOn(){
    document.getElementById("text").style.color = "green";
    document.getElementById("text").style.position = "fixed";
    document.getElementById("text").style.left = "300px";       
}
        
function mouseOff(){
    document.getElementById("text").style.position = "auto";
    document.getElementById("text").style.color = "blue";               
}
<h1 id="text" onmouseover= "mouseOn()" onmouseout = "mouseOff()"> Move the cursor over the text to see it change </h1>

You most likely just need to use fontSize and fontFamilly to make this work您很可能只需要使用fontSizefontFamilly来完成这项工作

function mouseOn() {
 ...
 document.getElementById("text").fontSize = "22px";
 document.getElementById("text").fontFamilly = "Font Familly";
}

and then revert it on mouseOff :然后在mouseOff上还原它:

function mouseOff() {
 ...
 document.getElementById("text").fontSize = "initial font size";
 document.getElementById("text").fontFamilly = "initial familly size";
}

In your css, you can write在你的css中,你可以这样写

#text {
  position: auto;
  color: blue;
}
#text.changed {
  position: fixed;
  left: 300px;
  color: green;
  font-size: 20px; /* here goes your font size */
  font-family: sans-serif; /* here goes your font family */
}

Don't forget to make sure you have positioned #text 's parent (eg position: relative ), otherwise positioning on #text won't work.不要忘记确保您已经定位了#text的父级(例如position: relative ),否则在#text上定位将不起作用。

/* text's-parent { position: relative } */
/* #text.changed { ...the styles you need } */

Then, in js然后,在js

function mouseOn() {
  document.getElementById("text").classList.add('.changed');
}
function mouseOff() {
  document.getElementById("text").classList.remove('.changed');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用鼠标 hover 使用 CSS 时,我试图让按钮中的文本更改颜色 - I am trying to make the text in a button change color when I hover over in with the mouse using CSS 鼠标悬停时如何更改段落文本? - How do I change a paragraph text when mouse hover over? 将鼠标悬停在文本上时如何制作文本? - How do I make text text when you hover the mouse over it? 将鼠标悬停在地图上时,如何使地图的区域变色? - How can I make the map's regions change color when hover the mouse over it? 我想在文本上显示图像。 假设当我将光标放在文本上时需要显示图像 - I want to show image over text. Suppose when i am putting cursor over text then need to show image 当我在文本上使用 hover 时,我想让图像出现。 我集成了一些 JavaScript,但它仍然无法正常工作 - I want to make an image appears when I hover over text. I integrated few JavaScripts but it's still not working 当我将鼠标悬停在文本上时,它会改变颜色并且颜色保持不变,我该如何做到这一点? - How do I make it so when I hover over my text it changes color and the color stays changed? 带有文本标签的按钮:即使在文本标签上方,如何保持悬停颜色? - Button with text label: How do I keep the hover color even when over text label? 如何在此js中更改鼠标悬停时星星的颜色??? - how can i change the color of the stars on mouse hover in this js??? 为什么 <li> 元素悬停在图像上后将鼠标悬停在元素上时,颜色和宽度不会改变吗? - Why does the <li> element not change color and width when I hover the mouse over it after hovering over the image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM