dongsimu4422 2013-08-10 14:12
浏览 23
已采纳

根据导航中选择的链接更改div的背景图像

i'd like to change the background image of a div depending on wich link is selected in my navigation!

exemple : let's say I have a menu item called :

#nav li.menu-item-59

when the link is selected it changes to

#nav li.menu-item-59.selected a

I'd like that whenever one of the menu item is selected the background image of the div footer change to a different file...

I've read some articles about sibling operators but can't seem to make it work and I'm not sure it is the best way to go .. can anyone help? thanks ! :D

  • 写回答

2条回答 默认 最新

  • dongyuan9292 2013-08-10 14:18
    关注

    It looks like you're using JS to add the class of selected to the menu. At the same time you're adding that, also add the the menu item name to the footer. something like:

    var menuName = $(this).attr('id');
    $('.footer').addClass(menuName);
    

    Then in your css for the footer, add the class to the end of the element:

    .footer.menu-item-59 {
      // background goes here
    }
    

    based on your fiddle below, try:

    $(window).scroll(function(){   
      for(var i = 0; i < sections.length; i++)
        if($(window).scrollTop() +5 >= sections[i].top &&
        $(window).scrollTop() <= sections[i].bottom){
          sections[i].link.addClass('selected')
          .siblings().removeClass('selected');
          var selection = 'selected' + i; // new stuff starts here
          $('footer #flag').removeAttr('class');
          $('footer #flag').addClass(selection);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂