如何使这些菜单项链接到其他页面?

时间:2019-03-31 01:01:14

标签: javascript html css href

我试图确保在彩色框上单击“主页”将带您到yayitworks.html,但是当我说<p href="yayitworks.html">HOME</p>时,单击时不会转到yayitworks.html。 。能够单击实际文本并转到yayitworks.html会很好,但是最好能够仅单击框上的任何位置(通常更好的ux)

var open = false;

function Drop(n) {
    var i;
    if (open == false) {
        for (i = n; i < 5; i++) {
            Drp(i)
        }
        open = true
    } else if (open == true) {
        for (i = n; i < 5; i++) {
            Cls(i)
        }
        open = false
    }
}

function Drp(n) {
    var elem = document.getElementsByClassName("menu-con")[n];
    var pos = -1 * window.innerHeight - n * 100;
    var id = setInterval(frame, 5);

    function frame() {
        if (pos >= -10) {
            clearInterval(id);
            elem.style.top = 0 + 'px';
        } else {
            pos += 10;
            elem.style.top = pos + 'px';
        }
    }
}

function Cls(n) {
    var elems = document.getElementsByClassName("menu-con")[n];
    var poss = 0;
    var ids = setInterval(frames, 5);

    function frames() {
        if (poss <= -1 * window.innerHeight) {
            clearInterval(ids);
            elems.style.top = -1 * window.innerHeight + 'px';
        } else {
            poss += -7 - n * 2;
            elems.style.top = poss + 'px';
        }
    }
}
* {
    box-sizing: border-box;
    max-width: 100%;
    font-family: 'PT Sans Narrow', sans-serif;
    font-weight: bold;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
    background-image: url(background.png);
    background-color: black;
}

.menu-icon {
    width: 50px;
    height: 50px;
    position: fixed;
    top: 0;
    right: 0;
    margin: 10px 15px;
    transform: scale(0.8);
    padding: 0;
    cursor: pointer;
    z-index: 20
}

.menu-bar {
    width: 50px;
    height: 5px;
    background: rgb(190, 190, 190);
    position: absolute;
    transition: all 0.3s;
    font-weight:bold;
    font-size:50px
}

.menu-bar1 {
    margin-top: 9px
}

.menu-bar2 {
    margin-top: 23px
}

.menu-bar3 {
    margin-top: 37px
}

.menu-icon.hover .menu-bar1 {
    -webkit-transform: rotate(45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu-icon.hover .menu-bar2 {
    opacity: 0
}

.menu-icon.hover .menu-bar3 {
    -webkit-transform: rotate(-45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu {
    width: 100%;
    height: 100%;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
}

.menu-con {
    -webkit-flex-grow: 1;
    flex-basis: 0;
    flex-grow: 1;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-around;
    position: relative;
    top: -100%;
    transition: all 0.5s
}

.menu-con p:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    opacity: 1;
    background: rgba(0, 0, 0, 0);
    -webkit-transition: all 0.5s;
    transition: all 0.5s;
}

.menu-con:hover p:before {
    background: rgba(0, 0, 0, 0.2)
}

.menu-con p {
    height: 20px;
    -webkit-align-self: center;
    color: white;
    font-size: 25px;
    z-index: 2;
    cursor: pointer
}

@media screen and (max-width: 600px) {
    .menu-con {
        min-width: 50%
    }
}

@media screen and (max-width: 350px) {
    .menu-con {
        min-width: 100%
    }
}
<html>
    <head>
    <title>Ben Cohen</title>
<link href=style.css rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=PT+Sans+Narrow" rel="stylesheet">
        <link href="animate.css" rel=stylesheet>
<script src="main.js"></script>
           </head>
    <body>
<div class="menu-icon" onclick="this.classList.toggle('hover');Drop(0)">
    <div class="menu-bar menu-bar1"></div>
    <div class="menu-bar menu-bar2"></div>
    <div class="menu-bar menu-bar3"></div>
</div>
 
<div class="menu">
    <div class="menu-con" style="background:red;" href="yayitworks.html">
        <p>HOME</p>
    </div>
    <div class="menu-con" style="background:blue">
        <p>PORTFOLIO</p>
    </div>
    <div class="menu-con" style="background:darkorange;">
        <p>UNDECIDED</p>
    </div>
    <div class="menu-con" style="background:green;">
        <p>HOMEWORK</p>
    </div>
    <div class="menu-con" style="background:white;">
        <p style="color:black">TEST PAGE</p>
    </div>
</div>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

首先:您的班级名称拼写错误,应该是menu-icon而不是menu-con

第二个:
<div class="menu-icon" style="background:red;" href="yayitworks.html"> <p>HOME</p> </div>

这应该是<a>标记,而不是<div>,如下所示:

<a class="menu-icon" style="background:red;" href="yayitworks.html"> <p>HOME</p> </a>

第三步:不要忘记在CSS的display: block;类中添加.menu-icon,使其具有与当前相同的作用。

答案 1 :(得分:0)

只需在{strong> CSS 中将<p>更改为<a href="">,然后将p更改为a,我希望能对您有所帮助。谢谢

var open = false;

function Drop(n) {
    var i;
    if (open == false) {
        for (i = n; i < 5; i++) {
            Drp(i)
        }
        open = true
    } else if (open == true) {
        for (i = n; i < 5; i++) {
            Cls(i)
        }
        open = false
    }
}

function Drp(n) {
    var elem = document.getElementsByClassName("menu-con")[n];
    var pos = -1 * window.innerHeight - n * 100;
    var id = setInterval(frame, 5);

    function frame() {
        if (pos >= -10) {
            clearInterval(id);
            elem.style.top = 0 + 'px';
        } else {
            pos += 10;
            elem.style.top = pos + 'px';
        }
    }
}

function Cls(n) {
    var elems = document.getElementsByClassName("menu-con")[n];
    var poss = 0;
    var ids = setInterval(frames, 5);

    function frames() {
        if (poss <= -1 * window.innerHeight) {
            clearInterval(ids);
            elems.style.top = -1 * window.innerHeight + 'px';
        } else {
            poss += -7 - n * 2;
            elems.style.top = poss + 'px';
        }
    }
}
* {
    box-sizing: border-box;
    max-width: 100%;
    font-family: 'PT Sans Narrow', sans-serif;
    font-weight: bold;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
    background-image: url(background.png);
    background-color: black;
}

.menu-icon {
    width: 50px;
    height: 50px;
    position: fixed;
    top: 0;
    right: 0;
    margin: 10px 15px;
    transform: scale(0.8);
    padding: 0;
    cursor: pointer;
    z-index: 20
}

.menu-bar {
    width: 50px;
    height: 5px;
    background: rgb(190, 190, 190);
    position: absolute;
    transition: all 0.3s;
    font-weight:bold;
    font-size:50px
}

.menu-bar1 {
    margin-top: 9px
}

.menu-bar2 {
    margin-top: 23px
}

.menu-bar3 {
    margin-top: 37px
}

.menu-icon.hover .menu-bar1 {
    -webkit-transform: rotate(45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu-icon.hover .menu-bar2 {
    opacity: 0
}

.menu-icon.hover .menu-bar3 {
    -webkit-transform: rotate(-45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu {
    width: 100%;
    height: 100%;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
}

.menu-con {
    -webkit-flex-grow: 1;
    flex-basis: 0;
    flex-grow: 1;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-around;
    position: relative;
    top: -100%;
    transition: all 0.5s
}

.menu-con a:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    opacity: 1;
    background: rgba(0, 0, 0, 0);
    -webkit-transition: all 0.5s;
    transition: all 0.5s;
}

.menu-con:hover a:before {
    background: rgba(0, 0, 0, 0.2)
}

.menu-con a {
    height: 20px;
    -webkit-align-self: center;
    color: white;
    font-size: 25px;
    z-index: 2;
    text-decoration: none;
}

@media screen and (max-width: 600px) {
    .menu-con {
        min-width: 50%
    }
}

@media screen and (max-width: 350px) {
    .menu-con {
        min-width: 100%
    }
}
<html>
    <head>
    <title>Ben Cohen</title>
<link href=style.css rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=PT+Sans+Narrow" rel="stylesheet">
        <link href="animate.css" rel=stylesheet>
<script src="main.js"></script>
           </head>
    <body>
<div class="menu-icon" onclick="this.classList.toggle('hover');Drop(0)">
    <div class="menu-bar menu-bar1"></div>
    <div class="menu-bar menu-bar2"></div>
    <div class="menu-bar menu-bar3"></div>
</div>
 
<div class="menu">
    <div class="menu-con" style="background:red;">
        <a href="yayitworks.html">HOME</a>
    </div>
    <div class="menu-con" style="background:blue">
        <a href="">PORTFOLIO</a>
    </div>
    <div class="menu-con" style="background:darkorange;">
        <a href="">UNDECIDED</a>
    </div>
    <div class="menu-con" style="background:green;">
        <a href="">HOMEWORK</a>
    </div>
    <div class="menu-con" style="background:white;">
        <a href="" style="color:black">TEST PAGE</a>
    </div>
</div>
    </body>
</html>

答案 2 :(得分:0)

href属性位于<a>标签上,而不是<div>标签上。我更新了您的代码,向您展示了它的外观。

var open = false;

function Drop(n) {
    var i;
    if (open == false) {
        for (i = n; i < 5; i++) {
            Drp(i)
        }
        open = true
    } else if (open == true) {
        for (i = n; i < 5; i++) {
            Cls(i)
        }
        open = false
    }
}

function Drp(n) {
    var elem = document.getElementsByClassName("menu-con")[n];
    var pos = -1 * window.innerHeight - n * 100;
    var id = setInterval(frame, 5);

    function frame() {
        if (pos >= -10) {
            clearInterval(id);
            elem.style.top = 0 + 'px';
        } else {
            pos += 10;
            elem.style.top = pos + 'px';
        }
    }
}

function Cls(n) {
    var elems = document.getElementsByClassName("menu-con")[n];
    var poss = 0;
    var ids = setInterval(frames, 5);

    function frames() {
        if (poss <= -1 * window.innerHeight) {
            clearInterval(ids);
            elems.style.top = -1 * window.innerHeight + 'px';
        } else {
            poss += -7 - n * 2;
            elems.style.top = poss + 'px';
        }
    }
}
* {
    box-sizing: border-box;
    max-width: 100%;
    font-family: 'PT Sans Narrow', sans-serif;
    font-weight: bold;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
    background-image: url(background.png);
    background-color: black;
}

.menu-icon {
    width: 50px;
    height: 50px;
    position: fixed;
    top: 0;
    right: 0;
    margin: 10px 15px;
    transform: scale(0.8);
    padding: 0;
    cursor: pointer;
    z-index: 20
}

.menu-bar {
    width: 50px;
    height: 5px;
    background: rgb(190, 190, 190);
    position: absolute;
    transition: all 0.3s;
    font-weight:bold;
    font-size:50px
}

.menu-bar1 {
    margin-top: 9px
}

.menu-bar2 {
    margin-top: 23px
}

.menu-bar3 {
    margin-top: 37px
}

.menu-icon.hover .menu-bar1 {
    -webkit-transform: rotate(45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu-icon.hover .menu-bar2 {
    opacity: 0
}

.menu-icon.hover .menu-bar3 {
    -webkit-transform: rotate(-45deg) scaleX(0.7);
    margin-top: 22px;
}

.menu {
    width: 100%;
    height: 100%;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
}

.menu-con {
    -webkit-flex-grow: 1;
    flex-basis: 0;
    flex-grow: 1;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-around;
    position: relative;
    top: -100%;
    transition: all 0.5s
}

.menu-con p:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    opacity: 1;
    background: rgba(0, 0, 0, 0);
    -webkit-transition: all 0.5s;
    transition: all 0.5s;
}

.menu-con:hover p:before {
    background: rgba(0, 0, 0, 0.2)
}

.menu-con p {
    height: 20px;
    -webkit-align-self: center;
    color: white;
    font-size: 25px;
    z-index: 2;
    cursor: pointer
}

@media screen and (max-width: 600px) {
    .menu-con {
        min-width: 50%
    }
}

@media screen and (max-width: 350px) {
    .menu-con {
        min-width: 100%
    }
}
<html>
    <head>
    <title>Ben Cohen</title>
<link href=style.css rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=PT+Sans+Narrow" rel="stylesheet">
        <link href="animate.css" rel=stylesheet>
<script src="main.js"></script>
           </head>
    <body>
<div class="menu-icon" onclick="this.classList.toggle('hover');Drop(0)">
    <div class="menu-bar menu-bar1"></div>
    <div class="menu-bar menu-bar2"></div>
    <div class="menu-bar menu-bar3"></div>
</div>
 
<div class="menu">
    <a href="yayitworks.html">
        <div class="menu-con" style="background:red;">
            <p>HOME</p>
        </div>
    </a>
    <div class="menu-con" style="background:blue">
        <p>PORTFOLIO</p>
    </div>
    <div class="menu-con" style="background:darkorange;">
        <p>UNDECIDED</p>
    </div>
    <div class="menu-con" style="background:green;">
        <p>HOMEWORK</p>
    </div>
    <div class="menu-con" style="background:white;">
        <p style="color:black">TEST PAGE</p>
    </div>
</div>
    </body>
</html>

自HTML5起,您可以将div放入a-tag中。请参阅此参考:

https://css-tricks.com/snippets/jquery/make-entire-div-clickable/