我需要你的指示:
我正在尝试使用切换的div创建一个页面。当我点击它时,当我点击菜单中的链接并从其他页面点击此链接时,我需要它们打开。当我点击菜单中的一个锚点时,我也需要它切换正确的div并滚动到锚点。
我尝试了或多或少有效的东西:
我的第一个问题是我必须在链接上单击两次才能使动画生效。我发现$("#")。live(' click',function(){..}可以成为解决方案,但我不知道如何使用它。
第二个问题,动画从一个页面到另一个页面不起作用
第三个问题,我认为我的代码语法不好而且可以优化,但我不知道如何......
这是我的HTML:
jQuery(document).ready(function() {
$('.toggle-content').hide();
jQuery('.toggle-head').click(function() {
$(this).siblings('.toggle-content').slideToggle('slow');
$(this).toggleClass('clicked');
});
jQuery('a.ancre-head').click(function() {
var hash = document.location.hash.replace('#', '');
$('#' + hash).each(function() {
if (hash.indexOf($(this).attr("href")) != 1 && $('#' + hash).siblings('.toggle-content').is(":hidden")) {
$('#' + hash).siblings('.toggle-content').slideDown('slow');
$('#' + hash).toggleClass('clicked');
$('html, body').animate({
scrollTop: $('#' + hash).offset().top - 20
}, 500);
} else if (hash.indexOf($(this).attr("href")) != 1 && $('#' + hash).siblings('.toggle-content').is(":visible")) {
$('html, body').animate({
scrollTop: $('#' + hash).offset().top - 20
}, 500);
}
});
return false;
});
jQuery('a.ancre').click(function() {
var hash = document.location.hash.replace('#', '');
$('#' + hash).each(function() {
if (hash.indexOf($(this).attr("href")) != 1 && $('#' + hash).parent('.toggle-content').is(":hidden")) {
$('#' + hash).parent('.toggle-content').slideDown('slow');
$('#' + hash).parents().siblings('.toggle-head').toggleClass('clicked');
$('html, body').animate({
scrollTop: $('#' + hash).offset().top - 20
}, 500);
} else if (hash.indexOf($(this).attr("href")) != 1 && $('#' + hash).parent('.toggle-content').is(":visible")) {
$('html, body').animate({
scrollTop: $('#' + hash).offset().top - 20
}, 500);
}
});
return false;
});
});
//SMOOTHSCROLL//
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
我的JS:
$
如果你能帮我理解我错在哪里,我会很高兴的!
谢谢!
Katapult
答案 0 :(得分:0)
以下纯HTML / CSS解决方案可满足您的所有要求,但动画除外。
它使用CSS :active
和:target
伪类的组合来实现所需的行为。为了工作,HTML也必须进行一些修改。奖励是,这些项目现在也可通过键盘导航获得:)
.toggle-content > * {
height: 0;
overflow: hidden;
}
.toggle-head [id]:target,
.toggle-head:focus .toggle-content > *,
.toggle-head:target .toggle-content > * {
height: auto;
}

<header id="header">
<!--Menu-->
<nav>
<ul id="menu">
<li><a href="gp.html"><h2>TITRE2</h2></a>
<ul>
<li><a class="ancre-head" href="#A">A</a>
</li>
<li><a class="ancre-head" href="#B">B</a>
<ul>
<li><a class="ancre" href="#B-01">B-01</a>
</li>
<li><a class="ancre" href="#B-02">B-02</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</header>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<div class="toggle">
<div id="A" class="toggle-head" tabindex="0">
<div class="toggle-head-content">
<h3>A</h3>
</div>
<div class="toggle-content">
<p>A content</p>
</div>
</div>
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
</p>
<p>
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
</p>
<p>
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat Lorem
</p>
<p>
ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do
</p>
<p>
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing
</p>
<p>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<div class="toggle">
<div id="B" class="toggle-head" tabindex="0">
<div class="toggle-head-content">
<h3>B</h3>
</div>
<div class="toggle-content">
<p>B</p>
<div id="B-01" tabindex="0">B-01</div>
<div id="B-02" tabindex="0">B-02</div>
</div>
</div>
</div>
&#13;