【发布时间】:2017-01-09 19:38:45
【问题描述】:
请有人帮帮我。
我有一个页面,上面有几个隐藏的部分,还有一系列链接到并显示这些部分的其他页面。最初页面只是直接跳转到锚点,但我做到了页面从顶部滚动到该部分的位置;问题是页面在跳转到页面顶部然后滚动之前会短暂跳转到锚点。
这是我的代码:
function toggle(id) {
var element = document.getElementById(id);
var text = document.getElementById("arrow" + id);
if (element) {
var display = element.style.display;
if (display == "none" || display == '') {
element.style.display = "block";
text.innerHTML = "▲";
} else {
element.style.display = "none";
text.innerHTML = "▼";
}
}
};
jQuery(document).ready(function() {
jQuery(window.location.hash).show();
if (window.location.hash) {
setTimeout(function() {
jQuery('html, body').scrollTop(0,0).show();
jQuery('html, body').animate({
scrollTop: jQuery(window.location.hash).offset().top
-75}, 1000)
}, 0);
}
});
这是一页:
<p><a href="http://204.128.208.7/automated-transfers-users/#inventoryPriceMaintenance">Effect on Auto-Transfers</a></p>
这是它链接到的页面的一部分:
<h4 class="blueToggle" onclick="toggle('inventoryPriceMaintenance')">An Incorrect Setting in Inventory Price Maintenance<a id="arrowinventoryPriceMaintenance">▼</a></h4>
<div id="inventoryPriceMaintenance" class="hiddencontent">
<p style="margin-left: 2em; margin-bottom: .3em;">Navigate to the back-screen</p>
<a href="http://204.128.208.7/wp-content/uploads/auto-transfer-7.png"><img class="aligncenter" src="http://204.128.208.7/wp-content/uploads/auto-transfer-7.png" alt="" width="85%" height="85%" /></a>
<p style="text-align: justify; margin-left: 2em; line-height: 1.5em; margin-bottom: .625em;">The item will not be included on automatically generated transfers to a store if the second character in the fourth column (<strong>COMP</strong>) is…</p>
<ul style="margin-left: 5em; line-height: 1.5em;">
<li><strong>D</strong> = discontinued (an item cannot be transferred to a store at which it is discontinued, but it can be transferred from that store)</li>
<li><strong>S</strong> = special order</li>
<li><strong>X</strong> = item has been discontinued everywhere (only relevant at store 00)</li>
</ul>
<p style="text-align: justify; margin-left: 2em;">An item will not auto-transfer if it is not authorized at the specific store</p>
<p style="text-align: justify; margin-left: 5em; line-height: 1.5em; margin-top: -1em; margin-bottom: 0px">Go to the back-screen and see if the store is on the list (a list of authorized stores can also be found on the Inventory Inquiry screen)</p>
<h4 class="blueToggle" onclick="toggle('inventoryPriceMaintenance')" style="margin-top: .3em">Hide -</h4>
</div>
我已经寻找解决方案,并尝试了 e.preventDefault();并返回 false,但都没有工作,我不知道还有什么可以尝试的。
【问题讨论】:
-
浏览器将在 JS 加载之前移动到锚点位置。我不相信你可以用 JS 取消它,尽管你可以使用查询字符串或不存在的锚来解决它(然后 JS 可以计算出它们的关系)
-
什么是不存在的锚?
-
例如在你的页面上你有
#thisAnchor,但你只是链接到.com#this,然后JS会做类似window.location.hash + "Anchor"的事情来获得真正的锚。这样浏览器不会移动页面(#this不存在),但你的 JS 仍然可以移动到它,因为它知道添加一致的最终部分。
标签: javascript jquery wordpress