简体   繁体   English

onmouseout和onmouseover

[英]onmouseout and onmouseover

I am working on homework that involves working with javascript. 我正在从事涉及使用javascript的作业。 Part of my homework assignment is to use the event handlers onmouseout and onmouseouver. 我的家庭作业的一部分是使用事件处理程序onmouseout和onmouseouver。 What is supposed to happen when the user hovers over a specific div element, the font size grows by 25%, and when the user mouses out of the div element, the font size goes back to normal. 当用户将鼠标悬停在特定的div元素上时,字体大小将增加25%,并且当用户将鼠标移出div元素时,字体大小将恢复正常。 My question is, is it possible to incorporate both an onmouseover function and an onmouseout function into one function? 我的问题是,是否可以将onmouseover函数和onmouseout函数都合并到一个函数中? Somehow that is what my teacher wants us to do. 我的老师希望我们以某种方式做到这一点。 I have this started so far. 到目前为止,我已经开始了。

function FontSize(x)
{
    x.style.fonstSize = large;
}

I'm also thinking this isnt the correct code to make the font 25% larger, but I'm not sure how to really incorporate an onmouseout in this function. 我还认为这不是使字体扩大25%的正确代码,但是我不确定如何在此功能中真正整合onmouseout。

As a teacher myself, I am 99% sure that by "one function" the instructor means one general-purpose function to change the font size, not one function which uses conditional statements to work backwards and figure out whether it should be doing onmouseout or onmouseover. 我本人是一名老师,我99%确信,通过“一个功能”,讲师意味着一个通用的功能来更改字体大小,而不是一个使用条件语句向后工作并弄清楚它是否应该在onmouseout上进行操作的功能。鼠标悬停。

Your script should contain: 您的脚本应包含:

function resize(elem, percent) { elem.style.fontSize = percent; }

Your HTML should contain: 您的HTML应包含:

<div onmouseover="resize(this, '125%')" onmouseout="resize(this, '100%')"
Text within div..
</div>

Note: Situations such as here, are exactly why JavaScript has the keyword "this"--to save us from needing to use complicated document.getElementById() statements. 注意:诸如此类的情况正是JavaScript具有关键字“ this”的原因-从而使我们不必使用复杂的document.getElementById()语句。

You can use "%" property for controlling font-size as described here with the following code. 您可以使用所描述的控制字体大小“%”属性在这里用下面的代码。

 document.getElementById("div1").onmouseover = function() {        
            document.getElementById("div1").style.fontSize  = "125%"
                        };

 document.getElementById("div1").onmouseout = function() {        
            document.getElementById("div1").style.fontSize  = "100%";
                        };

Here is the working jsfiddle : http://jsfiddle.net/LxhdU/ 这是工作中的jsfiddle: http : //jsfiddle.net/LxhdU/

Yes you can. 是的你可以。 Call the same function on both events, and pass a parameter to indicate whether the fontsize should increase or decrease. 在两个事件上调用相同的函数,并传递一个参数以指示fontsize应该增加还是减少。

ChangeFontSize = function(element, shouldIncreaseFontsize)
{
    var small=14;
    var large = small * 1.25;

    if(shouldIncreaseFontsize) {
         element.style.fontSize = large + "px";
    }
    else {
         element.style.fontSize = small + "px";
    }

}

http://jsfiddle.net/TMHbW/1/ http://jsfiddle.net/TMHbW/1/

It is possible you do not need to call both the events on the element explicitly instead extension you create will do that.Extend the Element 's prototype. 您可能不需要显式调用元素上的两个事件,而是您创建的扩展名可以做到。扩展Element的原型。 Jquery also does similar to this. jQuery也与此类似。

Ref Prototype 参考原型

See Fiddle:- http://jsfiddle.net/4fs7V/ 参见小提琴:-http: //jsfiddle.net/4fs7V/

Element.prototype.hover= function( fnOver, fnOut ) {

    this.onmouseover=fnOver;
    this.onmouseout=fnOut || fnOver;
        return this;
    };


document.getElementById('test').hover(function(){
                                         //do your mouseover stuff
                                                },
                                     function(){
                                           //do your mouseout stuff
                                     });

Update 更新资料

Same can be achieved with just one function too:- 只需一个功能也可以实现相同的功能:-

Hover me 悬停我

.largeFont {
    font-size:125%;
}

Element.prototype.hover = function (fnOver, fnOut) {
    this.onmouseover = fnOver;
    this.onmouseout = fnOut || fnOver;
    return this;
};

document.getElementById('test').hover(changeMe);

function changeMe()
{
    if(this.hasAttribute('class'))
    {
        this.removeAttribute('class');
    }
    else
    {
         this.setAttribute('class', 'largeFont');
    }
}

I'd do something simple like the following. 我会做一些简单的事情,如下所示。 The large and small values can be whatever you need them to be for the font size to work or they can be variables you've defined in prior code. largesmall值可以是您需要的大小,字体大小才能起作用,也可以是您在先前代码中定义的变量。

Demo: http://jsfiddle.net/lucuma/EAbYn/ 演示: http : //jsfiddle.net/lucuma/EAbYn/

 function doHover(e) {
    if (e.type=='mouseover') {
          this.style.fontSize = "large";   
    } else {

          this.style.fontSize = "small";  
    }
}

var el = document.getElementById('myelement')
el.onmouseout =doHover;
el.onmouseover=doHover;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM