jQuery-小屏幕导航.下一步和基于当前窗口URL的Prev按钮HREF



我有侧边栏,它在iPad屏幕上消失,而较小的下一个和先前的箭头代替。

基本上,我想取下当前页面URL,将其从域上缝下来,然后将其与侧边栏中的锚标签进行比较。找到正确的页面后,在侧边栏中的"当前页面"标签之前,将"以前的"图标HREF填充了锚固式标签的href,然后将"下一个"图标HREF填充后面的href href。

sidebar.html (仅几行,但实际侧栏更长( -

<nav class="main-nav">
<ul class="nav nav-pills nav-stacked">
<li>
<a href="{% url 'index:pg_1' %}" id="sub_1" ><h5>
{{title_1}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_2' %}" id="sub_1" ><h5>
{{title_2}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_3' %}" id="sub_1"><h5>
{{title_3}}<span class=></span></h5></a>
</li>
<li>
<a href="{% url 'index:pg_4' %}" id="sub_1"><h5>
{{title_4}}<span class=></span></h5></a>
</li>

图标 -

<i class="fa fa-angle-left fa-5x prev" aria-hidden="true">
  <a class="left" href="#"></a>
</i>
<i class="fa fa-angle-right fa-5x next" aria-hidden="true">
<a href="#" class="right"></a> 
</i>

jQuery-

$(function(){
console.log("nav for sm screens is ready");
// var url; 
var x; 
var url; 
var page_url = window.location.pathname;
page_url.split('/');
console.log(page_url);
url = $("a#sub_1").attr("href"); // Gets all 'a' tags with id of 'sub_1' (all of them) 
console.log("we have urls "); 
for (x in url){                // loops through them
    if (page_url == url){      // matches page url with a url in sidebar 
      console.log("we have a url that is the same as the page url")
      console.log(url)
      if ($(this).closest("li a").prev().length) {  // code usually breaks down here, checking if an anchor tag exists before current selection or not
        console.log("there are no links before this")
        $(".prev").hide();   // if no anchor tag exists before this tag, hide previous.
        console.log("left is hidden");
        $(".right").prop("href", $(this).next("a").prop("href"));  // set 'next' to href of next anchor tag.  
        console.log("we have next link");
      };
    };
};
});

我已经尝试使用closest()parent()parents()和其他几个变体,但似乎无效。

这很模棱两可,很难说明一个例子。这是我想到的:

https://jsfiddle.net/twisty/lrzo071q/8/

html

<nav class="main-nav">
  <i class="fa fa-angle-left fa-5x prev" aria-hidden="true"></i>
  <ul class="nav nav-pills nav-stacked">
    <li><a href="./page-1/" id="sub-1" class="sub-link"><h5>1<span class=""></span></h5></a></li>
    <li><a href="./page-2/" id="sub-2" class="sub-link"><h5>2<span class=""></span></h5></a></li>
    <li><a href="./page-3/" id="sub-3" class="sub-link"><h5>3<span class=""></span></h5></a></li>
    <li><a href="./_display/" id="sub-4" class="sub-link"><h5>4<span class=""></span></h5></a></li>
  </ul>
  <i class="fa fa-angle-right fa-5x next" aria-hidden="true"></i>
</nav>

javascript

$(function() {
  console.log("Small Screen Navigation Setup");
  var page_url = window.location.pathname,
    urlParts = page_url.split('/');
  console.log("URL:", page_url, "URL Parts:", urlParts);
  $(".sub-link").each(function(k, el) {
    var href = $(el).attr("href");
    console.log("Compare:", href, "to: ./" + urlParts[1] + "/");
    if (href == "./" + urlParts[1] + "/") { 
      console.log("Target URL found in Window URL");
      console.log("Current URL:", href);
      if ($(el).parent().is("li:last")) {
        console.log("Hidding Next.");
        $(".next").hide();
      }
      if ($(el).parent().is("li:first")) {
        console.log("Hidding Prev.");
        $(".prev").hide();
      }
    } else {
      console.log("Target URL not found.");
    }
  });
});

控制台

Small Screen Navigation Setup
URL: /_display/ URL Parts: Array [ "", "_display", "" ]
Target URLs collected: Array [ "./page-1/", "./page-2/", "./page-3/", "./_display/" ]
Compare: ./page-1/ to: ./_display/
Target URL not found.
Compare: ./page-2/ to: ./_display/
Target URL not found.
Compare: ./page-3/ to: ./_display/
Target URL not found.
Compare: ./_display/ to: ./_display/
Target URL found in Window URL
Current URL: ./_display/
Hidding Next.

当然,这在您的网站上会有所不同。如果您包括一些输出样本,则此示例可能更具体。基本上,我们希望将href中的值与当前URL进行比较。使用.each(),我们可以在每个<a>上迭代,然后将href属性的值与我们从window.location.pathname收集的特定字符串进行比较。

我们找到了比赛后,我们要测试它是:first还是:last元素。我们可以通过使用.is()进行测试来做到这一点。由于我们正在迭代<a>元素,并且需要测试<li>项目,因此我们需要使用.parent()来查看元素父元素。

您会看到输出类似:

< 1 2 3 4

该示例的第二个测试将是将./_display/移动到其他链接:

  <ul class="nav nav-pills nav-stacked">
    <li><a href="./_display/" id="sub-1" class="sub-link"><h5>1<span class=""></span></h5></a></li>
    <li><a href="./page-2/" id="sub-2" class="sub-link"><h5>2<span class=""></span></h5></a></li>
    <li><a href="./page-3/" id="sub-3" class="sub-link"><h5>3<span class=""></span></h5></a></li>
    <li><a href="./page-4/" id="sub-4" class="sub-link"><h5>4<span class=""></span></h5></a></li>
  </ul>

这将具有以下输出:

1 2 3 4 >

希望这会有所帮助。

最新更新