Jump to content

Add Audio to Header

Recommended Posts

Dear Community,

I am looking for some help to add a Audio (on and off) to the header of my website, like the picture attached in red

Does anybody know how to do this with some code lines? My website is www.khaia.co

Unfortunately Squarespace doesn't provide this option yet, they only offer to add an audio track outside of the header. 

Thank you in advance

Capture.PNG

Edited by SarahKhaia
Link to comment

Hello Joseph,

Yes I would like something different, Squarespace offers a way to add Audio blocks directly in the content of the website - I would like to add the Audio blocks on the menu at the top ie in the header (like the pic attached from my first post) - Squarespace confirmed this is not something they do but I can ask the community for some help with some lines of code

 

Link to comment

So the steps are the following:

1. You create an audio block in the top of the footer section

2. You add this code in the Injections menu > Footer:

<script>
   const headerAudio = document.querySelector('#footer-sections .sqs-block-audio');
   document.querySelector('#header .header-display-desktop .header-title').insertAdjacentHTML('afterEnd', headerAudio.innerHTML);
</script>  

3. You can also add a CSS in the custom CSS(to avoid ever displaying an audio in the footer, till javascript does it's job)

Custom CSS:

#footer-sections .sqs-block-audio {
  display: none;
}

For mobile version I am not sure if you want to display the audio so I did not cover that. The styling for the audio(spaces around) needs to be done manually from CSS

.header-display-desktop .sqs-block-audio {
   margin: 0 2%;
}

 

Edited by joseph81

Jozsef Kerekes - Front-end developer and Squarespace enthusiast
My Blog: https://ui-workarounds.com
If you like my answer, please give me an upvote/like. Highly appreciated.

 

 

Link to comment

Thank you very much Joseph!

So unfortunately it doesn't work on my side

1. I created an audio block in the top of the footer section (cf. photo 1)

2. I added the code in the Injections menu (cf. photo 2)

3.  I added a CSS in the custom CSS (cf. photo 3)

The result => the audio disappeared from the audio block i initially added (photo 1), and everytime i want to add it back it just doesn't show up. Plus it stays in the bottom section of my website and doesn't show up in the header

Do you know what I did wrong?

Thank you!

1.PNG

2.PNG

3.PNG

Link to comment

Yes, the problem is that you added the script in the Advanced tab but you should add the script in the Injections panel > Footer 

Advanced tab adds script in top of document instead of bottom of the document. So the script executes before the element were loaded in the html, so it throws an error.

image.png.6fc21775aed7252058d8b66c3c0019ad.png

image.png.97e5a64187af68b8fad543c7ac67a448.png

Edited by joseph81

Jozsef Kerekes - Front-end developer and Squarespace enthusiast
My Blog: https://ui-workarounds.com
If you like my answer, please give me an upvote/like. Highly appreciated.

 

 

Link to comment
  • 1 month later...

Hi!

I am having a similar issue and wanted to ask your help. I want to make a simple audio playback icon to be displayed on the right hand side of the header. It should only be displayed on my home page, not on any other pages. 

I tried to apply the code as described in this thread, and have gotten so far:image.thumb.png.54af294e7b5440ac10e24ec4007b0ee6.png

What remains is changing the design to a desired icon, and placeing it on the right hand side. I have included a simple sketch of what I want the header of my home page to look like. It would also be nice if the icon displays a difference betwee on/off as shown in the paratheses in my sketch.

 

IMG_4125.thumb.jpg.67bf884c272e02daaef95a3d962d2a41.jpg

Hope you might be able to help out!

Best,

Sigurd

image.thumb.png.54af294e7b5440ac10e24ec4007b0ee6.png

Link to comment
On 12/16/2022 at 6:56 PM, sihole said:

Hi!

I am having a similar issue and wanted to ask your help. I want to make a simple audio playback icon to be displayed on the right hand side of the header. It should only be displayed on my home page, not on any other pages. 

I tried to apply the code as described in this thread, and have gotten so far:image.thumb.png.54af294e7b5440ac10e24ec4007b0ee6.png

What remains is changing the design to a desired icon, and placeing it on the right hand side. I have included a simple sketch of what I want the header of my home page to look like. It would also be nice if the icon displays a difference betwee on/off as shown in the paratheses in my sketch.

 

IMG_4125.thumb.jpg.67bf884c272e02daaef95a3d962d2a41.jpg

Hope you might be able to help out!

Best,

Sigurd

image.thumb.png.54af294e7b5440ac10e24ec4007b0ee6.png

Hi, 

This is possible. This morning I Just solved a similar case. If you share link to your site, we can adjust the code for your case easier

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment
23 hours ago, tuanphan said:

Hi, 

This is possible. This morning I Just solved a similar case. If you share link to your site, we can adjust the code for your case easier

Hi,

Wonderful, thank you so much! Here is my site

dahlia-chameleon-xdem.squarespace.com

Since my messag I have made a store, menaning I am not quite sure if the right hand side of the header is the best placement for the audio playback icon... happy to get ideas, and perhaps learn how I can change the placement in the code.

Best,

Sigurd

Link to comment
2 minutes ago, sihole said:

Hi,

Wonderful, thank you so much! Here is my site

dahlia-chameleon-xdem.squarespace.com

Since my messag I have made a store, menaning I am not quite sure if the right hand side of the header is the best placement for the audio playback icon... happy to get ideas, and perhaps learn how I can change the placement in the code.

Best,

Sigurd

Guessing you might need a password as my site is not yet live. Password is sketchingsound

Perhaps the right hand side of the header will work for placement if the audio icon is put next to the cart. Another solution might be to put it after my name in the middle of the header. 

Best,

Sigurd

Link to comment
On 12/21/2022 at 7:57 PM, sihole said:

Guessing you might need a password as my site is not yet live. Password is sketchingsound

Perhaps the right hand side of the header will work for placement if the audio icon is put next to the cart. Another solution might be to put it after my name in the middle of the header. 

Best,

Sigurd

Add to Settings > Advanced > Code Injection > Footer

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"/>
<script>
  $(document).ready(function() { 
  // audio icon 
  $('<a id="play-pause-button" class="fa fa-play"></a>').appendTo('.header-actions--right .header-actions-action');
	});
</script>
<script>
$(document).ready(() => {
    var audio = new Audio("https://www.w3schools.com/tags/horse.ogg");
    audio.onended = function() {
        const list = document.querySelectorAll('#play-pause-button');
        for (let i=0; i<list.length; i++) {
            $(list[i]).removeClass('fa-pause');
            $(list[i]).addClass('fa-play');
        }
    };
         
    window.addEventListener('mousedown', (e) => {
        const btn = e.target
        if (btn.id === 'play-pause-button') {
            console.log(3534);
            if($(btn).hasClass('fa-play'))
               {
                 $(btn).removeClass('fa-play');
                 $(btn).addClass('fa-pause');
                 audio.play();
               }
            else
               {
                 $(btn).removeClass('fa-pause');
                 $(btn).addClass('fa-play');
                 audio.pause();
               }
        }
        
    });
});
</script>

Replace https://www.w3schools.com/tags/horse.ogg

with your audio url

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Thanks a lot, this code works nice!! There is however a couple of minor tweaks I am wondering if might be applied:

1) can playback start automatically on page open as default..?

2) icon: instead of play / pause buttons, may the icons be like this (transparent)? (where sound on is the one with soundwaves - this should then show on page open as playback starts automatically)

image.png.bb9fb9f01ee9a60f80174a23ba3a7693.pngimage.png.d8603f1fbbffcd849b1b4d938e4240e1.png

3) placement: now the icon is directly below the cart. I wonder if a better placement might be in the middle of the header, below "The music of Sigurd Hole", but then we would need a customized option for mobile as this would not look so good there I think..

image.thumb.png.5c98e3c76bfa3cb8e32a627f3ff7e2a9.png

 

image.png

Link to comment
On 12/29/2022 at 9:26 PM, sihole said:

Thanks a lot, this code works nice!! There is however a couple of minor tweaks I am wondering if might be applied:

1) can playback start automatically on page open as default..?

2) icon: instead of play / pause buttons, may the icons be like this (transparent)? (where sound on is the one with soundwaves - this should then show on page open as playback starts automatically)

image.png.bb9fb9f01ee9a60f80174a23ba3a7693.pngimage.png.d8603f1fbbffcd849b1b4d938e4240e1.png

3) placement: now the icon is directly below the cart. I wonder if a better placement might be in the middle of the header, below "The music of Sigurd Hole", but then we would need a customized option for mobile as this would not look so good there I think..

image.thumb.png.5c98e3c76bfa3cb8e32a627f3ff7e2a9.png

 

image.png

Hi,

#1. No. This won't run on all devices. Many devices do not allow Video/Audio with Sound play automatically.

#2. Here I used FontAwesome Icon. If you can find an icon what you want here, we can give the adjust code.

List icon: https://fontawesome.com/

#3. I don't see icon now. Can you add the code & enable cart icon?

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment
On 1/2/2023 at 12:19 PM, sihole said:

#1. I see.

#2. These fonts would be nice:

<i class="fa-light fa-volume"></i>

<i class="fa-light fa-volume-off"></i>

#3. Now cart is enabled. Does it have to be enabled for the audio playback buttons to show?

Thanks!

Hi. Can you share link to page where you get these font icon code? I need to check their unicode code

 

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

If possible, it would be great if the sound icon is not dependent on the cart icon being enabled in the header. Ideally I would learn how to change the placement of the sound icon in the CSS myself to be able to experiment- if it is not too complicated..

Thanks again for helping out!

Link to comment
On 1/4/2023 at 5:54 PM, sihole said:

Thank you. But can you choose other icons? these are pro version

image.thumb.png.7f8b4847fc275cc9f1395e30e1899f06.png

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.