Using vanilla Javascript to change the colour of a Id in a SVG file once scrolled past a certain point












0















As the title says I have a object in my HTML which is the SVG



      <object data="images/parallax_2/buildings_1.2.svg" type="image/svg+xml" id="building_1">


That SVG is of a building I made in illustrator. As I scroll down the page I want certain windows of that building to change colour as if someone inside were turning a light on.



Within the SVG file these are the windows I want to change:



    <g>
<polygon id="WINDOW_x5F_1" class="st107" points="583.3,1114.5 583.4,1047.9 607.4,1052 607.3,1118.7 "/>
<polygon class="st107" points="628.1,1122.3 628.2,1055.7 652.2,1059.8 652.1,1126.5 "/>
<polygon class="st107" points="672.9,1130.1 673,1063.4 697,1067.6 696.9,1134.2 "/>
<polygon class="st107" points="717.6,1137.8 717.7,1071.1 741.8,1075.3 741.6,1142 "/>
<polygon class="st107" points="762.4,1145.6 762.5,1078.9 786.5,1083.1 786.4,1149.7 "/>
<polygon class="st107" points="807.2,1153.4 807.3,1086.7 831.3,1090.9 831.2,1157.5 "/>
<polygon class="st107" points="851.9,1161.1 852.1,1094.5 876.1,1098.7 876,1165.3 "/>
<polygon class="st107" points="896.7,1168.9 896.8,1102.2 920.9,1106.4 920.7,1173.1 "/>
<polygon class="st107" points="941.5,1176.6 941.6,1110 965.6,1114.2 965.5,1180.8 "/>
</g>


As you can see I changed the ID of one of them so I can then target it with my Javascript on my index.html page:



    window.onscroll = function changeClass(){

var scrollPosY = window.pageYOffset | document.body.scrollTop;

// Get the Object by ID
var a = document.getElementById("building_1");

// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;

// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementsByClassName('st107');

// Set the class to something else
if(scrollPosY > 100) {
svgItem.className = ('st1071');
} else if(scrollPosY <= 100) {
svgItem.className = ('st1071');
}
}


Unfortunately this isn't working for me. I'm not getting any errors from this, just nothing is happening.



I was wondering if any of you would know how to succeed at this?



Any responses would be massively appreciated!!



Many, many thanks,
Adam










share|improve this question























  • Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

    – Nimit Shah
    Nov 13 '18 at 17:44






  • 1





    Do you have to use it as an object? Why don't you use it as an inline SVG?

    – enxaneta
    Nov 13 '18 at 19:34
















0















As the title says I have a object in my HTML which is the SVG



      <object data="images/parallax_2/buildings_1.2.svg" type="image/svg+xml" id="building_1">


That SVG is of a building I made in illustrator. As I scroll down the page I want certain windows of that building to change colour as if someone inside were turning a light on.



Within the SVG file these are the windows I want to change:



    <g>
<polygon id="WINDOW_x5F_1" class="st107" points="583.3,1114.5 583.4,1047.9 607.4,1052 607.3,1118.7 "/>
<polygon class="st107" points="628.1,1122.3 628.2,1055.7 652.2,1059.8 652.1,1126.5 "/>
<polygon class="st107" points="672.9,1130.1 673,1063.4 697,1067.6 696.9,1134.2 "/>
<polygon class="st107" points="717.6,1137.8 717.7,1071.1 741.8,1075.3 741.6,1142 "/>
<polygon class="st107" points="762.4,1145.6 762.5,1078.9 786.5,1083.1 786.4,1149.7 "/>
<polygon class="st107" points="807.2,1153.4 807.3,1086.7 831.3,1090.9 831.2,1157.5 "/>
<polygon class="st107" points="851.9,1161.1 852.1,1094.5 876.1,1098.7 876,1165.3 "/>
<polygon class="st107" points="896.7,1168.9 896.8,1102.2 920.9,1106.4 920.7,1173.1 "/>
<polygon class="st107" points="941.5,1176.6 941.6,1110 965.6,1114.2 965.5,1180.8 "/>
</g>


As you can see I changed the ID of one of them so I can then target it with my Javascript on my index.html page:



    window.onscroll = function changeClass(){

var scrollPosY = window.pageYOffset | document.body.scrollTop;

// Get the Object by ID
var a = document.getElementById("building_1");

// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;

// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementsByClassName('st107');

// Set the class to something else
if(scrollPosY > 100) {
svgItem.className = ('st1071');
} else if(scrollPosY <= 100) {
svgItem.className = ('st1071');
}
}


Unfortunately this isn't working for me. I'm not getting any errors from this, just nothing is happening.



I was wondering if any of you would know how to succeed at this?



Any responses would be massively appreciated!!



Many, many thanks,
Adam










share|improve this question























  • Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

    – Nimit Shah
    Nov 13 '18 at 17:44






  • 1





    Do you have to use it as an object? Why don't you use it as an inline SVG?

    – enxaneta
    Nov 13 '18 at 19:34














0












0








0








As the title says I have a object in my HTML which is the SVG



      <object data="images/parallax_2/buildings_1.2.svg" type="image/svg+xml" id="building_1">


That SVG is of a building I made in illustrator. As I scroll down the page I want certain windows of that building to change colour as if someone inside were turning a light on.



Within the SVG file these are the windows I want to change:



    <g>
<polygon id="WINDOW_x5F_1" class="st107" points="583.3,1114.5 583.4,1047.9 607.4,1052 607.3,1118.7 "/>
<polygon class="st107" points="628.1,1122.3 628.2,1055.7 652.2,1059.8 652.1,1126.5 "/>
<polygon class="st107" points="672.9,1130.1 673,1063.4 697,1067.6 696.9,1134.2 "/>
<polygon class="st107" points="717.6,1137.8 717.7,1071.1 741.8,1075.3 741.6,1142 "/>
<polygon class="st107" points="762.4,1145.6 762.5,1078.9 786.5,1083.1 786.4,1149.7 "/>
<polygon class="st107" points="807.2,1153.4 807.3,1086.7 831.3,1090.9 831.2,1157.5 "/>
<polygon class="st107" points="851.9,1161.1 852.1,1094.5 876.1,1098.7 876,1165.3 "/>
<polygon class="st107" points="896.7,1168.9 896.8,1102.2 920.9,1106.4 920.7,1173.1 "/>
<polygon class="st107" points="941.5,1176.6 941.6,1110 965.6,1114.2 965.5,1180.8 "/>
</g>


As you can see I changed the ID of one of them so I can then target it with my Javascript on my index.html page:



    window.onscroll = function changeClass(){

var scrollPosY = window.pageYOffset | document.body.scrollTop;

// Get the Object by ID
var a = document.getElementById("building_1");

// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;

// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementsByClassName('st107');

// Set the class to something else
if(scrollPosY > 100) {
svgItem.className = ('st1071');
} else if(scrollPosY <= 100) {
svgItem.className = ('st1071');
}
}


Unfortunately this isn't working for me. I'm not getting any errors from this, just nothing is happening.



I was wondering if any of you would know how to succeed at this?



Any responses would be massively appreciated!!



Many, many thanks,
Adam










share|improve this question














As the title says I have a object in my HTML which is the SVG



      <object data="images/parallax_2/buildings_1.2.svg" type="image/svg+xml" id="building_1">


That SVG is of a building I made in illustrator. As I scroll down the page I want certain windows of that building to change colour as if someone inside were turning a light on.



Within the SVG file these are the windows I want to change:



    <g>
<polygon id="WINDOW_x5F_1" class="st107" points="583.3,1114.5 583.4,1047.9 607.4,1052 607.3,1118.7 "/>
<polygon class="st107" points="628.1,1122.3 628.2,1055.7 652.2,1059.8 652.1,1126.5 "/>
<polygon class="st107" points="672.9,1130.1 673,1063.4 697,1067.6 696.9,1134.2 "/>
<polygon class="st107" points="717.6,1137.8 717.7,1071.1 741.8,1075.3 741.6,1142 "/>
<polygon class="st107" points="762.4,1145.6 762.5,1078.9 786.5,1083.1 786.4,1149.7 "/>
<polygon class="st107" points="807.2,1153.4 807.3,1086.7 831.3,1090.9 831.2,1157.5 "/>
<polygon class="st107" points="851.9,1161.1 852.1,1094.5 876.1,1098.7 876,1165.3 "/>
<polygon class="st107" points="896.7,1168.9 896.8,1102.2 920.9,1106.4 920.7,1173.1 "/>
<polygon class="st107" points="941.5,1176.6 941.6,1110 965.6,1114.2 965.5,1180.8 "/>
</g>


As you can see I changed the ID of one of them so I can then target it with my Javascript on my index.html page:



    window.onscroll = function changeClass(){

var scrollPosY = window.pageYOffset | document.body.scrollTop;

// Get the Object by ID
var a = document.getElementById("building_1");

// Get the SVG document inside the Object tag
var svgDoc = a.contentDocument;

// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementsByClassName('st107');

// Set the class to something else
if(scrollPosY > 100) {
svgItem.className = ('st1071');
} else if(scrollPosY <= 100) {
svgItem.className = ('st1071');
}
}


Unfortunately this isn't working for me. I'm not getting any errors from this, just nothing is happening.



I was wondering if any of you would know how to succeed at this?



Any responses would be massively appreciated!!



Many, many thanks,
Adam







javascript svg






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:55









MadamotMadamot

82




82













  • Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

    – Nimit Shah
    Nov 13 '18 at 17:44






  • 1





    Do you have to use it as an object? Why don't you use it as an inline SVG?

    – enxaneta
    Nov 13 '18 at 19:34



















  • Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

    – Nimit Shah
    Nov 13 '18 at 17:44






  • 1





    Do you have to use it as an object? Why don't you use it as an inline SVG?

    – enxaneta
    Nov 13 '18 at 19:34

















Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

– Nimit Shah
Nov 13 '18 at 17:44





Can you share the working code? Like building SVG and complete html/JS/CSS code! That would be helpful

– Nimit Shah
Nov 13 '18 at 17:44




1




1





Do you have to use it as an object? Why don't you use it as an inline SVG?

– enxaneta
Nov 13 '18 at 19:34





Do you have to use it as an object? Why don't you use it as an inline SVG?

– enxaneta
Nov 13 '18 at 19:34












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284787%2fusing-vanilla-javascript-to-change-the-colour-of-a-id-in-a-svg-file-once-scrolle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284787%2fusing-vanilla-javascript-to-change-the-colour-of-a-id-in-a-svg-file-once-scrolle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python