简体   繁体   English

减少到面向对象的javascript

[英]reduce into object orientated javascript

I have writen a little javascript. 我写了一点javascript。 It is one of my first and I am still learning. 这是我的第一个,我还在学习。 I would like to reduce the line count and make it more efficient. 我想减少行数并使其更有效率。 I believe that Object Orientated Programming will be my best choice. 我相信面向对象编程将是我的最佳选择。

The script assigns a class to a button on hover. 该脚本在悬停时为按钮分配一个类。 gets elements with that class and performs various if functions to determine if the src attribute of the image button should be changed. 获取具有该类的元素并执行各种if函数以确定是否应更改图像按钮的src属性。 The script also changes another image src attribute at the same time. 该脚本还同时更改另一个图像src属性。

I am wondering if I can somehow condense the logic of the if statements into just one or two, then using variables perform the src attribute changes. 我想知道我是否能以某种方式将if语句的逻辑压缩成一两个,然后使用变量执行src属性更改。 But I dont know how to go about this...? 但我不知道怎么回事......?

    //assign navButtons to var buttons (creates array)
var buttons = document.getElementsByClassName("navButton");

//set buttonHover function
function buttonOn(){
    if ( arrow == topArrow.mouseout ) {
        newArrow = document.getElementById("topArrow");
        newArrow.setAttribute("src", topArrow.mouseover);
        menuText.setAttribute("src", topArrow.text);
    }
    if ( arrow == rightArrow.mouseout ) {
        newArrow = document.getElementById("rightArrow");
        newArrow.setAttribute("src", rightArrow.mouseover);
        menuText.setAttribute("src", rightArrow.text);
    }
    if ( arrow == bottomArrow.mouseout ) {
        newArrow = document.getElementById("bottomArrow");
        newArrow.setAttribute("src", bottomArrow.mouseover);
        menuText.setAttribute("src", bottomArrow.text);
    }
    if ( arrow == leftArrow.mouseout ) {
        newArrow = document.getElementById("leftArrow");
        newArrow.setAttribute("src", leftArrow.mouseover);
        menuText.setAttribute("src", leftArrow.text);
    } 
}

//set buttonHover function
function buttonOff(){
    if ( arrow != topArrow.mouseout ) {
        resetArrow = document.getElementById("topArrow");
        resetArrow.setAttribute("src", topArrow.mouseout);
        menuText.setAttribute("src", start.text);
    }
    if ( arrow != rightArrow.mouseout ) {
        resetArrow = document.getElementById("rightArrow");
        resetArrow.setAttribute("src", rightArrow.mouseout);
        menuText.setAttribute("src", start.text);
    }
    if ( arrow != bottomArrow.mouseout ) {
        resetArrow = document.getElementById("bottomArrow");
        resetArrow.setAttribute("src", bottomArrow.mouseout);
        menuText.setAttribute("src", start.text);
    }
    if ( arrow != leftArrow.mouseout ) {
        resetArrow = document.getElementById("leftArrow");
        resetArrow.setAttribute("src", leftArrow.mouseout);
        menuText.setAttribute("src", start.text);
    }
}

//for each instance of buttons, assign class "active" onmouseover
for(var i = 0; i < buttons.length; ++i){
    buttons[i].onmouseover = function() {
        this.className = "active";
        arrow = document.getElementsByClassName("active");
        //get attribute
        arrow = arrow[0].getAttribute("src");
        console.log(arrow);
        buttonOn();
    };
}

//for each instance of buttons, remove class "active" onmouseout
for(var i = 0; i < buttons.length; ++i){
    buttons[i].onmouseout = function () {
        arrow = document.getElementsByClassName("active");
        //get attribute
        arrow = arrow[0].getAttribute("src");
        buttonOff();
        this.className = "";
    };
}

Any help would be ace! 任何帮助都是王牌!

The JS Fiddle JS小提琴

Don't handle everything in JS, you can simply set a background image on the available anchor tags without using an img tag and then change the background image on :hover (best would be to use sprites). 不要处理JS中的所有内容,您可以在不使用img标记的情况下在可用锚标记上设置背景图像,然后更改背景图像:hover(最好是使用sprite)。 The only part where JS should kick in would be to change the text image, but there also not by changing an img tag src attribute. JS应该启动的唯一部分是更改文本图像,但也不能通过更改img标记src属性。 You should preload all the text images as content (with a sensible alt text), position them over one another and then show/hide them according to what button was hovered. 您应该将所有文本图像预加载为内容(使用合理的替代文本),将它们放在彼此之上,然后根据悬停的按钮显示/隐藏它们。

Maybe I'll adjust your fiddle if I get to it but you could probably already optimize it with these information yourself. 也许我会调整你的小提琴,但是你可能已经用这些信息自己优化了它。

You could improve the buttonOn function a bit, by removing the if statements and replacing them with a call to a factory class of some kind. 您可以通过删除if语句并将其替换为对某种工厂类的调用来改进buttonOn函数。 For example: 例如:

function ArrowHandlerFactory(){    
    var self = this;

    self.Create = function(arrow) {        
        alert(arrow);
        if ( arrow == topArrow.mouseout ) {

            return new topArrowHandler();
        }
    }

    return {
        Create: self.Create
    }
}

var topArrowHandler = function(){
    var self = this;

    self.ArrowPressed = function() {
        newArrow = document.getElementById("topArrow");
    newArrow.setAttribute("src", topArrow.mouseover);
    menuText.setAttribute("src", topArrow.text);
    }    

    return {
        ArrowPressed: self.ArrowPressed
    }
}

var factory = new ArrowHandlerFactory();

//set buttonHover function
function buttonOn(){
    var handler = factory.Create(arrow);

    handler.arrowPressed();
}

The above isn't the complete code, but it gives you the basics to get started. 以上不是完整的代码,但它为您提供了入门的基础知识。

I'm not stating that the above is the best JavaScript ever, but the core idea of using a factory class, and then a specific class for each arrow direction, is still sound. 我并不是说上面是有史以来最好的JavaScript,但是使用工厂类的核心思想,然后是每个箭头方向的特定类,仍然是合理的。

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

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