简体   繁体   English

如何通过类似事件简化此Javascript

[英]How can I simplify this Javascript with similar events

On my homepage I have these five divs 我的主页上有这五个div

<table id="nav">
        <tr>

        <td><a href="coding.html"><div id="pagecl">C O D I N G</div></a></td>
        <td><div id="pagecl" class="high">A R T W O R K</div></td>
        <td><div id="logo"><img src="imageswebbing/icon.png"></div></td>
        <td><a href="extras.html"><div id="pagecl" class="more">E X T R A S</div></a></td>
        <td><a href="about.html"><div id="pagecl" class="last">A B O U T</div></a></td>

        </tr>
    </table>

this is the corresponding css: 这是对应的CSS:

#nav {
    margin:300px auto auto auto;
}

#pagecl {
    height:40px;
    width:200px;
    background-color:#151515;
    color:white;
    text-align:center;
    line-height:40px;
    opacity:0.7;
    font-family: Garamond;
    font-size:12px;
}

#logo {
    height:120px;
    width:120px;
}

And the javascript allows for slow fades with mouse enter and mouse leave, but if I only use the #pagecel id in the javascript, the intended effect is only seen on the first "Coding" div. 并且javascript允许通过鼠标进入和鼠标离开来缓慢地淡入淡出,但是如果我仅在javascript中使用#pagecel id,则预期效果只能在第一个“ Coding” div上看到。 That's why I added the class selectors for the other divs in the html and javascript. 这就是为什么我在html和javascript中为其他div添加类选择器的原因。 How do I condense this? 我该如何浓缩? There's probably a simple solution; 可能有一个简单的解决方案。 I apologize for being a newbie. 我很抱歉成为新手。

$(document).ready(function() {  

   $('#pagecl').mouseenter(function() {
       $(this).fadeTo("slow",1);
   });

   $('#pagecl').mouseleave(function() {
   $(this).fadeTo("slow",.7);
   });

   $('.high').mouseenter(function() {
       $(this).fadeTo("slow",1);
   });

   $('.high').mouseleave(function() {
   $(this).fadeTo("slow",.7);
   });

   $('.more').mouseenter(function() {
       $(this).fadeTo("slow",1);
   });

   $('.more').mouseleave(function() {
   $(this).fadeTo("slow",.7);
   });

   $('.last').mouseenter(function() {
       $(this).fadeTo("slow",1);
   });

   $('.last').mouseleave(function() {
   $(this).fadeTo("slow",.7);
   });


});

IDs is unique identifiers. ID是唯一的标识符。 As per your statement I have these five divs 根据您的陈述, 我有这五个div

    <td><a href="coding.html"><div id="pagecl">C O D I N G</div></a></td>
    <td><div id="pagecl" class="high">A R T W O R K</div></td>     

make them unique or you can use a class to identify them 使其具有独特性,或者您可以使用类来识别它们

You can optimize your jQuery code as 您可以将jQuery代码优化为

Use , to pass multiple selectors . 使用,传递多个选择器

$(document).ready(function () {
    $('#pagecl, .high, .more, .last').hover(function () {
        $(this).fadeTo("slow", 1);
    }, function () {
        $(this).fadeTo("slow", .7);
    });
});

Also use .hover() , its (selector).hover( handlerIn, handlerOut ) is shorthand for: 也可以使用.hover() ,其(selector).hover( handlerIn, handlerOut )是以下形式的简写:

$( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );

Use a class to describe what you're doing, then use that class as the selector. 使用一个类来描述您的工作,然后将该类用作选择器。

$('.menuItem').mouseenter(function() {
   $(this).fadeTo("slow",1);
});
$('.menuItem').mouseleave(function() {
   $(this).fadeTo("slow",.7);
});

Then you don't need to have a bunch of selectors - just add the 'menuItem' class to anything you want that effect happening on. 然后,您不需要一堆选择器-只需将'menuItem'类添加到您希望发生这种效果的任何内容。

if you are targeting modern browsers, you don't need javascript for this. 如果您定位的是现代浏览器,则不需要javascript。

use css transition with :hover. 使用CSS过渡与:hover。

CSS EXAMPLE: CSS示例:

#pagecl:hover{
  opacity:0;
 }

#pagecl {
 opacity:1;
 transition: opacity 1s;
 /*add vendor prefixes -webkit etc...*/
}

.

You can always consider using a couple of subroutines to abstract repetitive behaviour: 您始终可以考虑使用几个子例程来抽象重复性行为:

function setMouseEvents(selector){
    $(selector).mouseenter(function(){
       $(this).fadeTo("slow", 1);
    })
    $(selector).mouseleave(function() {
       $(this).fadeTo("slow",.7);
    });
}

setMouseEvents('#pagecl');
setMouseEvents('.high');
//and so on

Give each one a class (x) also: 还给每个人一个等级(x):

<div id="pagecl" class="last x">

notice you can have multiple classes (space separated) but not multiple id's... 请注意,您可以有多个类(以空格分隔),但不能有多个ID。

then write once: 然后写一次:

$('.x').mouseenter(function(){$(this).fadeTo("slow",1);});
$('.x').mouseleave(function(){$(this).fadeTo("slow",0.7);});

I guess your not aware of multiple classes. 我猜你不知道多个类。 In your problem using a class you can make it more flexible. 在使用类的问题中,您可以使其更灵活。

An element can have more than one class seperated by a space but only one ID 一个元素可以有多个用空格分隔的one class ,但只能有一个ID

<div class="more not me yes ..." id="unique"></div>

more is one class and not is second class and me is third class applied on to the div tag 更多是一类,不是第二类,而我是第三类,应用于div标签

mouseenter and mouseleave are = .hover(mouseenter,mouseleave); = .hover(mouseenter,mouseleave);和mouseleave是= .hover(mouseenter,mouseleave);

$('.menuItem').hover(
               function() {
                   $(this).fadeTo("slow",1);
               },function() {
                  $(this).fadeTo("slow",.7); 
});

So add this to all the elements that want a hover effect 因此,将其添加到所有需要悬停效果的元素中

example

<div id="pagecl" class="high menuItem">A R T W O R K</div>

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

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