简体   繁体   English

PHP和jQuery-根据主体类或滚动顶部更改href?

[英]PHP & jQuery - Change href depending on body class or scrolltop?

I would like the onclick of the logo to scroll to the top (home page) if clicked from the home page (one page scroll website) and if on an internal page then the logo link to remain as the site url. 如果希望从主页(一个页面滚动网站)单击,徽标的onclick滚动到顶部(主页),如果在内部页面上单击,则徽标链接保留为站点URL。

My client basically does not want the site to reload onclick of the logo from the home page. 我的客户基本上不希望网站从主页上重新加载徽标的clickclick。

What would be the best way to do this? 最好的方法是什么? I have thought of 2 ways so far... 到目前为止,我已经想到了两种方法...

  1. Replace the href attribute with the url + body class (my menu links use this and scroll to each section without reloading the page) 将href属性替换为url + body类(我的菜单链接使用此属性,并滚动到每个部分,而无需重新加载页面)
  2. If click on logo when body hasclass .home, scroll to the top of the window. 如果在正文具有class .home时单击徽标,请滚动到窗口顶部。 But I'm not sure if this would override the url set by the theme? 但是我不确定这是否会覆盖主题设置的网址吗?

This is a snippet of how the logo href is set from the partials/menus.php file... 这是片段/menus.php文件中如何设置徽标href的摘要...

$logoDiv = '<a href="'.esc_url( home_url( '/' ) ).'" class="navbar-brand" data-minheight="'.(($LOGO->logo_min == "") ? "20" : esc_attr($LOGO->logo_min)).'">';

I am using the Uncode theme on Wordpress. 我在Wordpress上使用Uncode主题。 And I am a newbie at PHP and jQuery... Just a suggestion as the best way to go about this would be greatly appreciated, and I can try and work through it myself! 而且我是PHP和jQuery的新手...对此,最好的建议是最好的建议,我可以自己尝试一下!

Thank you :) 谢谢 :)

You can use the below code 您可以使用以下代码

$logoDiv = '<a href="javascript:void(0)" class="navbar-brand" data-minheight="'.(($LOGO->logo_min == "") ? "20" : esc_attr($LOGO->logo_min)).'" onclick="window.scrollTo(0,0);">';

Hope it may help you 希望对您有帮助

You can check if your current link is the homepage before rendering the page: 您可以在呈现页面之前检查当前链接是否为主页:

$href = ($_SERVER["REQUEST_URI"]==home_url("","relative")?"#":esc_url( home_url( '/' ) ))

$logoDiv = '<a href="'.$href.'" class="navbar-brand" data-minheight="'.(($LOGO->logo_min == "") ? "20" : esc_attr($LOGO->logo_min)).'">';

If you wish to animate the scroll then you can add the following jQuery: 如果希望对滚动进行动画处理,则可以添加以下jQuery:

$("[href='#']").on("click", function (e) {
    e.preventDefault();
     $('html, body').animate({
        scrollTop: 0
     });
});

This would work on all links with href='#' if you want to limit it you'd need a more specific selector. 如果您想限制它,那么它将对所有带有href='#'链接有效,您需要一个更具体的选择器。

As your comment suggests, you can also do this with jQuery if you have a way to determine if your current page is the home-page. 就像您的注释所建议的那样,如果您有办法确定当前页面是否为主页,也可以使用jQuery进行此操作。

Your solution should work: 您的解决方案应该可以工作:

$(document).ready(function() { 
    if($(body).hasClass("home")){ 
        $(".navbar-brand[href='ldngrp.co']").attr('href', '#'); 
     }
}); 

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

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