所以问题在这里:我有一个相当长的页面,中间有一个iframe。现在,如果在iframe中点击了一个锚点,整个页面就会滚动到iframe,这正是我想要避免的。
这里有一个示例:http://jsfiddle.net/ymbV7/1/ 不要滚动页面,而是滚动iframe,直到可以看到“内容”菜单,并尝试任何链接(例如“特性”)。
我需要外部页面不滚动,而iframe必须正确滚动到单击的锚点。
有什么好的想法吗?
所以问题在这里:我有一个相当长的页面,中间有一个iframe。现在,如果在iframe中点击了一个锚点,整个页面就会滚动到iframe,这正是我想要避免的。
这里有一个示例:http://jsfiddle.net/ymbV7/1/ 不要滚动页面,而是滚动iframe,直到可以看到“内容”菜单,并尝试任何链接(例如“特性”)。
我需要外部页面不滚动,而iframe必须正确滚动到单击的锚点。
有什么好的想法吗?
target="myiframe"
,而是添加了一个闹钟函数来绕过默认实现(似乎会导致父级跳到iframe)。<a onclick="linkFunction(this, event);return false;"...
/// for scrolling iframe without jumping parent page
function linkFunction(atag, event) {
event.preventDefault();
var iframe = document.getElementById('myiframe');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var name = atag.href.split('#')[1]; // get the hash
var anchor = innerDoc.getElementsByName(name)[0]; // find the corresponding anchor tag
// get position of the anchor relative to the current scroll position
var offset = anchor.getBoundingClientRect().top
// jump scroll the iframe content to the anchor
iframe.contentWindow.scrollBy(0, offset)
}
<a href="#exactline">Click here</a>
<h2><a name="exactline">Linking Directly from Features</a></h2>
<p>To override this behaviour, certain standard techniques can be used. In particular, you will need to create named anchors in the body of the text at the point where you want to link to.
</p>
http://www.thesitewizard.com/html-tutorial/link-to-specific-line-or-paragraph.shtml
我尝试了,对我有效。$('body').on('click', 'a', function(e) {
if($(this).attr('href').startsWith('#')) {
e.preventDefault();
}
});
我希望它能帮助你,祝你好运!
尝试将您的内容放入这样的表格中:
<table cellpadding=0 cellspacing=0 height="100%" width="100%">
<tr>
<td>
Header
</td>
</tr>
<tr>
<td>
<iframe src="http://en.wikipedia.org/wiki/jQuery" height=600 width="90%"></iframe>
</td>
</tr>
<tr>
<td>
Footer
</td>
</tr>
</table>
请参考http://www.webdeveloper.com/forum/showthread.php?t=212032
是的,你必须使用JQuery观察链接并执行它。
$(document).on('click', 'A[href^="#"]', function(){
var hash = $(this).attr('href');
var o = $(hash);
if (o.length) {
// move it
$("html,body").stop().animate({ scrollTop: o.offset().top }, 300, 'swing');
if (window.frameElement) {
// it has parent window => stop bubbling (will not change document location)
return false;
}
}
});
我认为,如果你这样做:<a target="iframe名称" href="#锚点名称">点击这里</a>
那么它应该可以工作,因为链接会在iframe中打开,这也可能是为什么锚点会导致整个页面滚动到iframe的原因。希望我能帮上忙,并希望它可以正常工作! :)