I'm using that code:
$(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;
}
}
});
});
and <a href="#allcontentcontact">
to link to the div <div id="allcontentcontact"></div>
.
The problem is: "#allcontentcontact" is on my homepage, so the code works perfectly only when I'm on the homepage. But I have a fixed menu that links to other pages! So I need something that makes my a href links to that div when I'm on another page.
Thanks!
Thanks for the answers, I did it! :)
<script>
var url = "http://192.168.1.157/wp/";
$(function(){
if (location.href==url){
$('#menu #mylink').attr("href","#bluecontent");
$('#menu #mylink2').attr("href","#allcontentcontact");
}
});
</script>
The URL is strange because I'm coding on localhost, but the code works perfectly. :)
And the HTML
<a id="mylink" href="<?php bloginfo('url'); ?>#bluecontent"><span>Parceiros</span></a>
<a id="mylink2" href="<?php bloginfo('url'); ?>#allcontentcontact"><span>Contato</span></a>
It's almost done. All the links works perfectly taking me smoothly to their specified anchors.
The problem is: my script that slides to anchors on other pages include the #bluecontent and #allcontentcontact on the URL. Is it possible to delete that from the URL? Here is the code:
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},2000,function()
{
location.hash = target;
});
}
$('html, body').hide();
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
jump();
}, 0);
}else{
$('html, body').show();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.