Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Change background of div according to hash url

  • This topic is empty.
Viewing 13 posts - 16 through 28 (of 28 total)
  • Author
    Posts
  • #94712
    Carlos56
    Member

    Anyone?

    If it’s not possible to update the logo when accessing an item through a hash link, then is it possible to have the change fade in?

    #94773
    sheepysheep60
    Participant

    Hey sorry haven’t been on in a while. It sounds like you could really do with watching something like this:
    https://css-tricks.com/video-screencasts/85-best-practices-dynamic-content/

    not being awkward, but I’m not 100% sure either and I’m sure Chris goes through everything you’ll need to know, you’ll just need to apply what we’ve spoken about using a method he uses!

    #94774
    Carlos56
    Member

    Hey Sheepysheep60, thanks for your reply!

    I’ll give that a go, thanks for sharing.

    In the meantime, anyone with a solution, PLEASE share it! (-:

    Thanks

    #94798
    jamygolden
    Member

    “That fixes the revert to the first image when refreshing the page. It doesn’t however update the logo when an item is accessed via a hash link. Is this not possible?”

    I provided that solution earlier.

    #94819
    Carlos56
    Member

    You did?!

    Where!?!?

    I’m pretty sure I have included all the script you guys have suggested in this thread. I don’t think any suggestions have updated the logo when accessing items through the hash url.

    Thanks again!

    #94823
    jamygolden
    Member
    #94824
    Mottie
    Member

    Ahh, sorry the problem with my demo is I forgot to remove the ” e.preventDefault(); // prevent default link behaviour” line, try this updated demo.

    #94844
    Carlos56
    Member

    jamy_za, I’ve made your code look like this..




    function cssChangeBasedOnHash() {

    switch ( window.location.hash ) {
    case '#serv':
    var backgroundPosition = '0px -151px'
    break;

    case '#port':
    var backgroundPosition = '0px -302px'
    break;

    case '#contact':
    var backgroundPosition = '0px -453px'
    break;

    default: // fallback
    var backgroundPosition = '0 0'
    break;
    };

    console.log(window.location.hash);

    $('.logo-item').css('backgroundPosition', backgroundPosition);

    }; // cssChangeBasedOnHash()

    $(document).ready(function() {

    cssChangeBasedOnHash();


    $(window).bind( 'hashchange', function(){

    cssChangeBasedOnHash();

    }); // window.bind.hashchange

    }); // dom.ready

    It’s not working though? :(

    Just to clarify the ‘#serv’ is just referring to what goes in ‘ href=”” ‘ – within the HTML right?? I can see your code works in your demo, but for some reason It’s not happening at my end. Must be the code.. or my slider maybe..

    Also Mottie your method does allow me to refresh the page once accessed through a hash link and update the logo, but it doesn’t do it automatically.

    Could my slider have something to do with it?!

    I appreciate all the time you guys have given me- thank you.

    #94890
    Carlos56
    Member

    Edit: thought something had solved it but it’s not the case. Sorry.

    #94907
    jamygolden
    Member

    Example with backgroundColor changed to backgroundPosition

    I’ve used Chris’ frog sprite as the background image =)

    #94908
    Carlos56
    Member

    it’s not showing up :(

    #94910
    jamygolden
    Member

    It’s the same link – but I’ve fixed the href problem.

    #94933
    Carlos56
    Member

    @jamy_za thank you so much for all your help.

    It’s finally working – it’s quite interesting to see how different methods achieve things in different ways.

    Before, the change of logo would happen almost instantly after the click. It now happens with a delay which kinda coincides with the page load.

    I realise I’m pushing it ever so slightly and should’ve probably started paying you guys a long time ago, but literally just a quick example of how a fade transition could be introduced to work with @jamy-za ‘s solution, would be extremely appreciated!

    Thank you once again all.

    Carlos

Viewing 13 posts - 16 through 28 (of 28 total)
  • The forum ‘CSS’ is closed to new topics and replies.