[英]Left animation on click li
I have a menu structure: 我有一个菜单结构:
<ul id="creamenu" class="menuHolder">
<li><a id="news-1-menu" href="#/creative-events">news 1</a></li>
<li><a id="news-2-menu" href="#/creative-ajans">news 2</a></li>
<li><a id="news-3-menu" href="#/incentive-travel">news 3</a></li>
</ul>
<ul id="mainmenu" class="menuHolder">
<li><a id="about1-menu" href="#/hakkimizda">about 1</a></li>
<li><a id="about2-menu" href="#/haberler">about 2</a></li>
<li><a id="about3-menu" href="#/galeri">about 3</a></li>
<li><a id="about4-menu" href="#/referanslar">about 4</a></li>
<li><a id="about5-menu" href="#/iletisim">about 5</a></li>
</ul>
I have a content structure in same HTML file: 我在同一HTML文件中有一个内容结构:
<div id='news-1'>
<!-- content -->
<!-- content -->
</div>
<div id='news-2'>
<!-- content -->
<!-- content -->
</div>
I want, when i click a item eg creamenu item, go to div. 我想要,当我单击某个项目(例如Creamenu项目)时,转到div。 EG
例如
click news-1 item, go to news-1 div. 单击新闻1项,转到新闻1格。 Like this: http://codecanyon.net/item/fancyscroll/full_screen_preview/370241
像这样: http : //codecanyon.net/item/fancyscroll/full_screen_preview/370241
Is it possible it with pure jQuery? 纯jQuery有可能吗?
It's absolutely possible. 绝对有可能。 Try this:
尝试这个:
$('#creamenu a').click(function(e) {
var splitId = $(this).attr('id').split('-'),
contentId = splitId[0] + '-' + splitId[1];
e.preventDefault();
$('html, body').animate({
scrollTop: $('#' + contentId).offset().top
}, 'slow');
});
Using this specific approach, you will need to ensure that the id of your link in the #creamenu is prefaced with the id of its corresponding content (as you are doing now). 使用这种特定方法,您将需要确保#creamenu中链接的ID开头是其相应内容的ID(就像您现在所做的那样)。
It would also be more robust to just put the id of your content in the href of the #creamenu link. 只需将您的内容ID放在#creamenu链接的href中,就会更可靠。 That way, if the user doesn't have JS active, it would still jump to the content, albeit without an animation.
这样,如果用户没有激活JS,即使没有动画,它仍会跳至内容。 For example:
例如:
<li><a id="news-1-menu" href="#news-1">news 1</a></li>
Then, your JS would be as follows: 然后,您的JS将如下所示:
$('#creamenu a').click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset().top
}, 'slow');
});
Yes, just change your href
attributes to the ID of the element: 是的,只需将
href
属性更改为元素的ID:
<li><a id="news-1-menu" href="#news-1">news 1</a></li>
To scroll smoothly, try: 要平滑滚动,请尝试:
$('a[href^=#]').on('click',function(e) {
e.preventDefault();
var id = $(this).attr('href'),
top = $(id).offset().top;
$('html,body').animate({'scrollTop':top}, function() {
window.location = id;
});
});
You can, but you need some minor changes to your code. 可以,但是您需要对代码进行一些小的更改。
$('#creamenu li a').click(function(event){
event.preventDefault();
$position = $( $(this).attr('href') ).offset().top;
$('html, body').animate({scrollTop:$position}, 'fast');
});
And change link structure, so href is and id of an element you would like to scroll to. 并更改链接结构,因此href是您要滚动到的元素的ID。
You could also make a links looks like this: 您还可以使链接看起来像这样:
<li><a id="news-1-menu" href="#/creative-events" rel="#news-1">news 1</a></li>
And use rel atribute to find an element, You would like to scroll to: 并使用rel atribute查找元素,您想滚动到:
$('#creamenu li a').click(function(event){
event.preventDefault();
$position = $( $(this).attr('rel') ).offset().top;
$('html, body').animate({scrollTop:$position}, 'fast');
});
And if you are looking for a parallax effect: http://jessandruss.us/ , checkout tutorial here: http://www.ianlunn.co.uk/blog/code-tutorials/jquery-vertical-parallax-background/ or here: http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/ 如果您正在寻找视差效果: http : //jessandruss.us/ ,请在此处签出教程: http : //www.ianlunn.co.uk/blog/code-tutorials/jquery-vertical-parallax-background/或此处: http : //www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.