单击链接将页面返回到顶部

时间:2014-01-14 18:19:07

标签: html css hyperlink return

我设置了图片,所以当你点击它们时,它们会在它们下方显示不同的div。

我在这里找到了原始代码:http://www.dynamicdrive.com/forums/showthread.php?74782-Changing-content-in-Div-upon-Link-Click

这是我的代码:

#workSamples {
    width:960px;
    margin:auto;
}

#button1 {
    width:300px;
    height:236px;
    display:inline-block;
    float:left;
    margin:0px 10px 20px 10px;
    -webkit-box-shadow: 0px 0px 8px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 0px 8px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 0px 8px rgba(50, 50, 50, 0.75);
}
.b1 {
    width:300px;
    height:236px;
    overflow:hidden;
}
.b1 img {
    height:auto;
    -webkit-transition: margin 1s ease;
    -moz-transition: margin 1s ease;
    -o-transition: margin 1s ease;
    -ms-transition: margin 1s ease;
    transition: margin 1s ease;
}
.b1 img:hover {
    margin-top: -236px;
}

#button2 {
    width:300px;
    height:236px;
    display:inline-block;
    float:left;
    margin:0px 10px 0px 10px;
    -webkit-box-shadow: 0px 0px 8px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 0px 8px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 0px 8px rgba(50, 50, 50, 0.75);
}
#button3 {
    width:300px;
    height:236px;
    display:inline-block;
    float:left;
    margin:0px 10px 0px 10px;
    -webkit-box-shadow: 0px 0px 8px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 0px 8px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 0px 8px rgba(50, 50, 50, 0.75);
}

#button1:focus~#content #def,
#button2:focus~#content #def,
#button3:focus~#content #def,
#button4:focus~#content #def {display:none;}

#button1:focus~#content div:nth-child(2),
#button2:focus~#content div:nth-child(3),
#button3:focus~#content div:nth-child(4),
#button4:focus~#content div:nth-child(5) {display:block;}

#content {
    width:940px; 
    height:307px;
    color:black;
    font-size:10px;
    text-align:center;
    margin:20px auto;
    -webkit-box-shadow: 0px 0px 8px rgba(50, 50, 50, 0.75);
    -moz-box-shadow:    0px 0px 8px rgba(50, 50, 50, 0.75);
    box-shadow:         0px 0px 8px rgba(50, 50, 50, 0.75);
    overflow:hidden;
    background:red;
}

.caption {
    background:blue;
    width:200px;
    margin:auto;
    height:60px;
    background:#694264;
    -webkit-transition:all .3s ease-out;  
    -moz-transition:all .3s ease-out;  
    -o-transition:all .3s ease-out;  
    -ms-transition:all .3s ease-out;  
    transition:all .3s ease-out;  
    left:0;
    font-size:14px;
    padding:10px;
}

#content:hover .caption {
    -moz-transform:translateY(-100%);  
    -o-transform:translateY(-100%);  
    -webkit-transform:translateY(-100%);  
    transform:translateY(-100%);  
}

#faq,#her,#and,#enj {display:none;}

#faq:hover{
    background:red;
}

<section class="top" data-type="background" data-speed="10" class="pages">
        <section id="w">
            <div class="title">My Work</div>
                <div id="workSamples">
                    <a id="button1" href="#" tabindex="1"><div class="b1"><img src="Images/work/sample01.jpg" alt=""></div></a>
                    <a id="button2" href="#" tabindex="2"><div class="b1"><img src="Images/work/sample02.jpg"></div></a>
                    <a id="button3" href="#" tabindex="3"><div class="b1"><img src="Images/work/sample03.jpg"></div></a>
                    <div id="content"> 
                        <div id="def">This is where I put the default information. No clicks needed </div>
                        <div id="faq"><img src="Images/absolutebeauty.jpg">
                            <div class="caption">
                                <a href="http://www.absolutebeauty.ie">
                                    <h4>Absolute Beauty</h4><br>Visit the site</a>
                            </div>
                        </div>
                        <div id="her"><img src="Images/bestacoustic.jpg"></div>
                        <div id="and"><img src="Images/engagement.jpg"></div>
                    </div>
            </div>
        </section>
</section>

代码可以自行运行,但当我将其添加到我的网站时,现在当我点击第一个图片链接时,它会将我返回到页面顶部。

以下是我的网站:http://www.liddily.com

单击工作并单击任何图像,您将看到我的意思。

我不知道为什么会这样。有人可以帮帮我吗?

感谢阅读!

1 个答案:

答案 0 :(得分:1)

您已将链接设置为转到不存在的锚点,以便返回页面本身。 <a href="#">Return</a>

将这些链接指向实际位置,“刷新”效果将停止。

请注意,在该网站的示例中,它显示以下代码,其中包含哈希值#之后的位置:

<div id="button1"><a href="#FAQ"><img src="http://img62.imageshack.us/img62/3523/p42e.png"></a></div>
<div id="button2"><a href="#Her"><img src="http://img16.imageshack.us/img16/3742/lnam.png"></a></div>
<div id="button3"><a href="#And"><img src="http://img829.imageshack.us/img829/2347/kflq.png"></a></div>
<div id="button4"><a href="#Enjoy"><img src="http://img24.imageshack.us/img24/5111/zv9.png"></a></div>

<div id="content"> This is where I put the default information. No clicks needed </div

<a name="FAQ">
<div style="border: 1px dashed black;width: 300px;height:500px;position:fixed;left:750px;top:150px;}
FAQ content will go here.

然后该示例显示了要转到的链接的命名位置。你需要实现这种格式才能发挥作用。