Code snippets
Here are some things I think are interesting that I have used on this site. Just in case you're wondering how it works under the hood. And also so I know what I've been doing.
Here are some things I think are interesting that I have used on this site. Just in case you're wondering how it works under the hood. And also so I know what I've been doing.
Contents:
Uses: HTML,CSS
Source: me
Used on: wouldn't you like to know
HTML:
<a href="" class="hidden"></a>
CSS:
.hidden {
color: #fff;
text-decoration: none;
}
.hidden:hover{
color: #fff;
text-decoration: none;
}
Uses: HTML, CSS
Source: Jordan Finneran
Used on: D&D item page
HTML:
<details>
<summary> Header here
<span class="icon">🎲</span>
</summary>
Content here
</details>
CSS:
details {
user-select: none;
}
details>summary span.icon {
width: 24px;
height: 24px;
transition: all 0.3s;
margin-left: auto;
}
details[open] summary span.icon {
transform: rotate(180deg);
}
summary {
display: flex;
cursor: pointer;
}
summary::-webkit-details-marker {
display: none;
}
Uses: Javascript, HTML
Source: Tony Vincent, tweaked by me
Used on: secret~
HTML:
<div id="randomText"></div>
Javascript:
<script >
var r_text = new Array ();
r_text[0] = "text 1";
r_text[1] = "text 2";
r_text[2] = "text 3";
r_text[3] = "text 4";
var i = Math.floor(r_text.length * Math.random());
document.getElementById('randomText').innerHTML = r_text[i]
</script>
Uses: Javascript, HTML
Source: me, based partially on the previous snippet
Used on: D&D item page
HTML:
<button class="button" onclick="randomItem()">Give me an item</button> <br>
<div id="results"></div>
Javascript:
<script>
function randomItem(){
const rows = document.querySelector("#itemTable tbody").querySelectorAll("tr");
var i = Math.floor((rows.length-1) * Math.random())+1;
document.getElementById('results').innerHTML = "<br><table><tr>" + rows[i].innerHTML + "</tr></table>"
};
</script>
Uses: Javascript, html
Source: W3 schools
Used on: D&D item page
HTML:
<table id="itemTable">
<tr>
<th onclick="sortTable(0)">Item name ▲▼</th>
<th onclick="sortTable(1)">Rarity ▲▼</th>
<th onclick="sortTable(2)">Type ▲▼</th>
<th>Stats</th>
<th>Flavour description</th>
</tr>
...
</table>
Javascript:
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("itemTable");
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
// Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
I use this one to load the top menu on every page. This way, I can edit the menu in one location and all pages are updated.
Uses: Javascript, html
Source: frankensteined together
Used on: every page
HTML, where /assets/menu.js is the location of the javascript file containing the menu:
<header id="navheader"></header>
<script LANGUAGE="JAVASCRIPT" src="/assets/menu.js">
</script>
Javascript (in seperate file, in my case located at /assets/menu.js):
document.addEventListener("DOMContentLoaded",
document.getElementById('navheader').innerHTML = `
<!--replace by your own menu html-->
<a href= "https://lunellum.neocities.org/">Home</a> | [...]
`
)
;