简体   繁体   English

页面跳转-无法停止

[英]Page Jumping - Can't stop it

I'm making mock ups for a flash app using HTML pages - I'm using HTML pages to demonstrate it inside a browser, plus it looks better pixel for pixel, and I'm using jQuery for rich animations on some pages. 我正在为使用HTML页面的Flash应用程序制作实体模型-我正在使用HTML页面在浏览器中演示它,而且它的像素看起来更好,并且我在某些页面上使用jQuery制作丰富的动画。

I'm using normal <a> tags for my buttons which navigate through the HTML pages. 我在按钮上使用普通的<a>标签,这些标签可在HTML页面中导航。

But if I have scrolled halfway down the page and click on a button at the bottom of the page - when it goes to the next HTML page it jumps back to the top of the page - this is obviously going to happen. 但是,如果我已经滚动到页面的一半,然后单击页面底部的按钮-当它转到下一个HTML页面时,它会跳回到页面顶部-这显然会发生。

Is there any javascript or tricks that can prevent all <a> tags from jumping to the top of the page when navigating though my HTML pages. 浏览我的HTML页面时,是否有任何JavaScript或技巧可以阻止所有<a>标记跳至页面顶部。 Or even better, if I could give my <a> tags a class so it doesn't interfere with my jQuery animations - because some use <a> tags. 甚至更好的是,如果我可以给我的<a>标签一个类,这样它就不会干扰我的jQuery动画-因为有些人使用<a>标签。

My html pages are like so: 我的html页面是这样的:

page-1.html
page-2.html
page-3.html
page-4.html
page-5.html

I'm not actually using jQuery or JavaScript to navigate through my pages, so when it jumps to page-2.html from page-1.html - it always jumps to top. 我实际上并没有使用jQuery或JavaScript来浏览我的页面,所以当它从page-1.html跳到page-2.html时,它总是跳到顶部。

My <a> tags are using full href's <a href="page-2.html" class="stop-jumping" title="Page 2"><img ... /></a> 我的<a>标签使用完整的href的<a href="page-2.html" class="stop-jumping" title="Page 2"><img ... /></a>

I'm looking for something to include in the head of every HTML page to stop my page from moving when navigating using <a> tags. 我正在寻找要包含在每个HTML页面head ,以防止使用<a>标记导航时页面移动。

I assume your a tag doesn't have a proper href attribute and you just put "#" in it (which is bad from a semantic and usability point of view). 我假设您a标签没有适当的href属性,而您只是在其中添加了"#" (从语义和可用性的角度来看这很糟糕)。

Your browser is thinking you're clicking on an anchor, try to find it in the DOM, fail and then brings you back to the top of the page. 您的浏览器认为您正在单击锚点,尝试在DOM中找到它,然后失败,然后将您带回到页面顶部。

A way to avoid that is to bind a function on the click event of your a tags and call preventDefault() 避免的方法是一个功能绑定在你的点击事件a标签,并调用preventDefault()

$("a").click(function(event) {
  event.preventDefault();
}

See the jQuery doc. 请参阅jQuery文档。


Edit due to huge edit on question 由于对问题的大量修改而进行修改

You need to use HTML anchor. 您需要使用HTML锚点。 On each of your html pages, give an id on the element you want your users screen to be aligned on. 在每个html页面上,在您希望用户屏幕对齐的元素上提供一个ID。

<h1 id="the_place">The user screen will be aligned to this element</h1>

And on every link that leads to another page, add #the_place on the end of the href attribute 在指向另一个页面的每个链接上,在href属性的末尾添加#the_place

<a href="/page-2.html#the_place">Go to page 2</a>

That should do it =). 那应该做=)。

$("a").click(function(e) {
  e.preventDefault();
}

You could use 你可以用

return false 

at end of the function that handle your navigation or calling 在处理导航或调用的函数末尾

e.preventDefault and e.stopPropagation

that stop the normal behaviour of the event (in your case a click event on an anchor). 停止事件的正常行为(在您的情况下为锚点上的click事件)。

Yes when you have a click event return false 是,当您有点击事件时返回false

 $('a').click(function(){
    return false;
}

This will break the standard a tag event 这将破坏标准的标签事件

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

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