![]() ![]() |
Hi all I'm doing a little toolbox for a browser game called "Glitch".
Here is the plan in two pics:
As you can see there is button on the left, that opens the frame when clicked, not displaying it over the content, but moving everything to the right.
A gave try to a bunch of iframe scripts here, but all of them seem to add an "overlay" to the page, rather than actually being on the left of the default content. Here is the (mockup) url, I'm willing to load on the side:
|
![]() ![]() |
Tried to keep it simple.
(function() {
// slide animation
GM_addStyle("\
.slide_out {\
-moz-animation-duration: 3s;\
-moz-animation-name: slide_out;\
-moz-animation-fill-mode: forwards;\
}\
.slide_in {\
-moz-animation-duration: 3s;\
-moz-animation-name: slide_in;\
-moz-animation-fill-mode: forwards;\
}\
@-moz-keyframes slide_out {\
100% { -moz-transform: translateX(300px); }\
}\
@-moz-keyframes slide_in {\
0% { -moz-transform: translateX(300px); }\
100% { -moz-transform: translateX(0px); }\
}\
");
// floating button, iframe
GM_addStyle("\
#toggle_sidebar {\
border: solid 3px #003366;\
background-color: #006699;\
position: absolute;\
left: 0px;\
top: 350px;\
width: 20px;\
height: 50px;\
cursor: pointer;\
}\
#sidebar {\
border: none;\
width: 300px;\
height: 1000px;\
position: absolute;\
left: -300px;\
top: 0px;\
overflow: hidden;\
}\
");
var btn = document.createElement("div");
btn.id = "toggle_sidebar";
btn.addEventListener("click", toggleSidebar, false);
document.body.appendChild(btn);
var ifr = document.createElement("iframe");
ifr.id = "sidebar";
ifr.src = "http://glitchcompanion.appspot.com/lite";
document.body.appendChild(ifr);
var toggleState = false; // false - closed, true - opened
function toggleSidebar() {
document.body.className = toggleState ? "slide_in" : "slide_out";
toggleState = !toggleState;
}
})();
|
![]() ![]() |
Hey AmpliDude
Just one small thing to make it perfect,
Thanks for making it short and efficient, it's a lot easier to understand!
|
![]() ![]() |
I tweaked the script a bit:
Now it's all about the numbers, you need to play with them to achieve what you want. (function() {
var wiw = window.innerWidth;
var scale = 75; // scale by 75%
var tX = 300 - parseInt(wiw * (1 - scale/100)/2); // translate body by X value to fit the sidebar, assuming 300px for the iframe
// scale animation
GM_addStyle("\
.scale_out {\
-moz-animation-duration: 3s;\
-moz-animation-name: scale_out;\
-moz-animation-fill-mode: forwards;\
}\
.scale_in {\
-moz-animation-duration: 3s;\
-moz-animation-name: scale_in;\
-moz-animation-fill-mode: forwards;\
}\
@-moz-keyframes scale_out {\
100% { -moz-transform: translate(" + tX + "px, -" + (100-scale)/2 + "%) scale(" + scale/100 + "); }\
}\
@-moz-keyframes scale_in {\
0% { -moz-transform: translate(" + tX + "px, -" + (100-scale)/2 + "%) scale(" + scale/100 + "); }\
100% { -moz-transform: translate(0px, 0%) scale (1); }\
}\
");
// slide animation
GM_addStyle("\
.slide_out {\
-moz-animation-duration: 3s;\
-moz-animation-name: slide_out;\
-moz-animation-fill-mode: forwards;\
}\
.slide_in {\
-moz-animation-duration: 3s;\
-moz-animation-name: slide_in;\
-moz-animation-fill-mode: forwards;\
}\
@-moz-keyframes slide_out {\
100% { -moz-transform: translateX(300px); }\
}\
@-moz-keyframes slide_in {\
0% { -moz-transform: translateX(300px); }\
100% { -moz-transform: translateX(0px); }\
}\
");
// floating button, iframe
GM_addStyle("\
#toggle_sidebar {\
border: solid 3px #003366;\
background-color: #006699;\
position: absolute;\
left: 0px;\
top: 350px;\
width: 20px;\
height: 50px;\
cursor: pointer;\
}\
#sidebar {\
border: none;\
width: 300px;\
height: 1000px;\
position: absolute;\
left: -300px;\
top: 0px;\
overflow: hidden;\
}\
");
var btn = document.createElement("div");
btn.id = "toggle_sidebar";
btn.addEventListener("click", toggleSidebar, false);
//document.body.appendChild(btn);
document.querySelector("html").appendChild(btn);
var ifr = document.createElement("iframe");
ifr.id = "sidebar";
ifr.src = "http://glitchcompanion.appspot.com/lite";
//document.body.appendChild(ifr);
document.querySelector("html").appendChild(ifr);
var toggleState = false; // false - closed, true - opened
function toggleSidebar() {
document.body.className = toggleState ? "scale_in" : "scale_out";
document.getElementById("toggle_sidebar").className = toggleState ? "slide_in" : "slide_out";
document.getElementById("sidebar").className = toggleState ? "slide_in" : "slide_out";
toggleState = !toggleState;
}
})();
|
![]() ![]() |
Yeah that's something like that I tried too with scale() (without all the details you added!)
Found these "transitions", next to the css animation docs, could be the way to go with "transition: width".
Thanks again for the help, I'm sure this kind of function will be useful to a bunch of people on this forum :] |
![]() ![]() |
Hmm well there is something moving, but that is not our sidebar :p
// slide animation
GM_addStyle("\
.slide_out {\
width:800px;\
-moz-transition: width 2s;\
}\
.slide_in {\
width:0px;\
-moz-transition: width 2s;\
}\
");
Looks quite fun applied here:
|
![]() ![]() |
Hello guys I'm still looking for a way to do this thing :] |
![]() ![]() |
That game appears to require signing-up to be able to view the page of the actual game (and moreover they say it can take a few weeks to sign-up...), so I can't really test this very well. However, I agree that transitions are probably more suitable here than animations. So, here is a modification of AmpliDude's code to use transitions and avoid scaling (and otherwise match my style a little bit more) that might be of help:
(function() {
"use strict";
var sidebarWidth = '300px';
GM_addStyle(
"body, #toggle_sidebar, #sidebar {\n" +
" -moz-transition-duration: 0.5s;\n" +
" -moz-transition-property: left, right;\n" +
"}\n" +
"body {\n" +
" position: absolute;\n" +
" left: 0; right: 0;\n" +
"}\n" +
"body.slide_out, #toggle_sidebar.slide_out {\n" +
" left: " + sidebarWidth + ";\n" +
"}\n" +
"#toggle_sidebar {\n" +
" border: solid 3px #003366;\n" +
" background-color: #006699;\n" +
" position: fixed;\n" +
" left: 0px;\n" +
" bottom: 6px;\n" +
" width: 20px;\n" +
" height: 50px;\n" +
" cursor: pointer;\n" +
"}\n" +
"#sidebar {\n" +
" border: none;\n" +
" width: " + sidebarWidth + ";\n" +
" height: 1000px;\n" +
" position: absolute;\n" +
" left: -" + sidebarWidth + ";\n" +
" top: 0px;\n" +
" overflow: hidden;\n" +
"}\n" +
"#sidebar.slide_out {\n" +
" left: 0;\n" +
"}\n"
);
var btn = document.createElement("div");
btn.id = "toggle_sidebar";
btn.addEventListener("click", toggleSidebar, false);
//document.body.appendChild(btn);
document.querySelector("html").appendChild(btn);
var ifr = document.createElement("iframe");
ifr.id = "sidebar";
ifr.src = "http://glitchcompanion.appspot.com/lite";
//document.body.appendChild(ifr);
document.querySelector("html").appendChild(ifr);
var showSidebar = false; // false - closed, true - opened
function setClass(elem) {
if (showSidebar) {
elem.className += ' slide_out';
} else {
elem.className = elem.className.replace(/\s*slide_out/, '');
}
}
function toggleSidebar() {
showSidebar = !showSidebar;
setClass(document.body);
setClass(document.getElementById("toggle_sidebar"));
setClass(document.getElementById("sidebar"));
}
})();
|
![]() ![]() |
Wow, this is exactly it now!
Now actually it works perfectly everywhere... except in that game screen doh :\
I pasted the source from http://www.glitch.com/game/ if you have the time to have
EDIT: could be a position issue?
|
![]() ![]() |
They seem to be setting the width of the main div with javascript, which is causing problems. Try
(function() {
"use strict";
var sidebarWidth = '300px';
GM_addStyle(
"#client_div, #toggle_sidebar, #sidebar {\n" +
" -moz-transition-duration: 0.5s;\n" +
" -moz-transition-property: left, right;\n" +
"}\n" +
"#client_div {\n" +
" position: absolute;\n" +
" left: 0 !important;\n" +
" right: 0 !important;\n" +
" width: auto !important;\n" +
"}\n" +
"#client_div.slide_out, #toggle_sidebar.slide_out {\n" +
" left: " + sidebarWidth + " !important;\n" +
"}\n" +
"#toggle_sidebar {\n" +
" border: solid 3px #003366;\n" +
" background-color: #006699;\n" +
" position: fixed;\n" +
" left: 0px;\n" +
" bottom: 6px;\n" +
" width: 20px;\n" +
" height: 50px;\n" +
" cursor: pointer;\n" +
"}\n" +
"#sidebar {\n" +
" border: none;\n" +
" width: " + sidebarWidth + ";\n" +
" height: 1000px;\n" +
" position: absolute;\n" +
" left: -" + sidebarWidth + ";\n" +
" top: 0px;\n" +
" overflow: hidden;\n" +
"}\n" +
"#sidebar.slide_out {\n" +
" left: 0;\n" +
"}\n"
);
var btn = document.createElement("div");
btn.id = "toggle_sidebar";
btn.addEventListener("click", toggleSidebar, false);
//document.body.appendChild(btn);
document.querySelector("html").appendChild(btn);
var ifr = document.createElement("iframe");
ifr.id = "sidebar";
ifr.src = "http://glitchcompanion.appspot.com/lite";
//document.body.appendChild(ifr);
document.querySelector("html").appendChild(ifr);
var showSidebar = false; // false - closed, true - opened
function setClass(elem) {
if (showSidebar) {
elem.className += ' slide_out';
} else {
elem.className = elem.className.replace(/\s*slide_out/, '');
}
}
function toggleSidebar() {
showSidebar = !showSidebar;
setClass(document.getElementById("client_div"));
setClass(document.getElementById("toggle_sidebar"));
setClass(document.getElementById("sidebar"));
}
})();
|
![]() ![]() |
Amazing, it totally works!
Now I guess I have to finish the actual page inside that iframe
Come visit in game some time ;)
|
![]() ![]() |
Okay my tool in that frame is almost done, was faster than I expected :] Only thing is that I noticed this script only works in Firefox and not in Chrome. I'd like to make it available to everyone in the game, and I know a bunch of them use Chrome. I tried with TamperMonkey as well, but the button disappear, or just doesn't act at all.
|
![]() ![]() |
Ah, yeah, Chrome doesn't provide the GM_* functions. Here is an alternative for GM_addStyle that I picked up somewhere
function GM_addStyle(css) {
var head = document.getElementsByTagName('head')[0];
if (!head) { return; }
var style = document.createElement('style');
style.type = 'text/css';
try {
style.innerHTML = css;
} catch(e) {
style.innerText = css;
}
head.appendChild(style);
}
Also, for cross-browser transitions, one has to specify the property many times with different prefixes. E.g. -moz-transition-* for Firefox, -webkit-transition-* for Safari and Chrome, -o-transition-* for Opera, etc. |
![]() ![]() |
Thanks!
Now, same problem again, it works everywhere except for the game page, even with the last version of the code.
|
![]() ![]() |
The version that worked before no longer works (meaning they changed the page)? Or it still works with Firefox, but isn't yet working with Chrome? (I'm a bit confused about what code you're using since the last version I posted had assumed there was an element with the id |
![]() ![]() |
Sorry I wasn't clear at all
And after I noticed that, I just wanted to try with the previous "all sites" code,
|
![]() ![]() |
Hmm, well, to the extent that the file you pasted loads, it looks about the same in Chrome and Firefox to me. It obviously isn't loading fully though, which limits how much I can directly analyze the issue. However, Firefox (with Firebug) and Chrome both have tools that let you view and modify the DOM and applied styles ("Inspect Element" from the context menu). If you poke around a bit (perhaps with the two browsers open side-by-side), that might help you figure out why they are behaving differently. |
![]() ![]() |
Hey there!
|


