Tidy up some SCSS - html

I have some code that controls a charity area on a site. The charity area has many different charity sectors and these all have their own colours. I have attached my SCSS below but wondered if there is a better way to write and apply this. So far I just need each charity colour to be applied as a 'background-color' but be able to have a tint of each. My code works fine but I'm sure there is a better way of writing this.
Thank you for any help in advanced.
/**************************************
Charity Colours
****************************************/
$color-animals: #a9efea;
$color-babies: #fae08c;
$color-cancer: #f4c9c8;
$color-community: #b3ddf2;
$color-deaf: #9FC279;
$color-mental: #CB89D5;
$color-elderly: #CCDCD4;
$color-rescue: #F4BD88;
$color-medical: #D0879C;
$color-hospice: #F0D3FA;
$color-human: #D3E0AD;
$color-military: #CBC3AD;
$color-overseas: #96C0E5;
$color-sports: #939393;
.animals-bg {
background:$color-animals;
}
.babies-bg {
background:$color-babies;
}
.cancer-bg {
background:$color-cancer;
}
.community-bg {
background:$color-community;
}
.deaf-bg {
background:$color-deaf;
}
.mental-bg {
background:$color-mental;
}
.elderly-bg {
background:$color-elderly;
}
.rescue-bg {
background: $color-rescue;
}
.medical-bg {
background:$color-medical;
}
.hospice-bg {
background:$color-hospice;
}
.human-bg {
background:$color-human;
}
.military-bg {
background:$color-military;
}
.overseas-bg {
background:$color-overseas;
}
.sports-bg {
background:$color-sports;
}
// Colour Tint for Lighter Background
$color-tint: 0.3;
.animals-bg--tint {
background: rgba($color-animals, $color-tint);
}
.babies-bg--tint {
background: rgba($color-babies, $color-tint);
}
.cancer-bg--tint {
background: rgba($color-cancer, $color-tint);
}
.community-bg--tint {
background: rgba($color-community, $color-tint);
}
.deaf-bg--tint {
background: rgba($color-deaf, $color-tint);
}
.mental-bg--tint {
background: rgba($color-mental, $color-tint);
}
.elderly-bg--tint {
background: rgba($color-elderly, $color-tint);
}
.rescue-bg--tint {
background: rgba($color-rescue, $color-tint);
}
.medical-bg--tint {
background: rgba($color-medical, $color-tint);
}
.hospice-bg--tint {
background: rgba($color-hospice, $color-tint);
}
.human-bg--tint {
background: rgba($color-human, $color-tint);
}
.military-bg--tint {
background: rgba($color-military, $color-tint);
}
.overseas-bg--tint {
background: rgba($color-overseas, $color-tint);
}
.sports-bg--tint {
background: rgba($color-sports, $color-tint);
}

Modified SCSS:
$charity-colors: (
animals: #a9efea,
babies: #fae08c,
cancer: #f4c9c8,
community: #b3ddf2,
deaf: #9FC279,
mental: #CB89D5,
elderly: #CCDCD4,
rescue: #F4BD88,
medical: #D0879C,
hospice: #F0D3FA,
human: #D3E0AD,
military: #CBC3AD,
overseas: #96C0E5,
sports: #939393
);
#each $sector, $color in $charity-colors {
.#{$sector}-bg {
background: $color;
}
.#{$sector}-bg--tint {
background: rgba($color, $color-tint);
}
}

Related

HTML class name as the link to the LESS variable

I have a set of LESS variables with colors:
#blue: #0e9bd0;
#green: #009646;
#red: #f81010;
I use class names like this:
.color-blue {
color: #blue;
}
.border-blue {
border-color: #blue;
}
.bg-blue {
background: #blue;
}
Is it possible to generate rules automatically for each color?
Something like below?
.color-#{name} {
color: ##name;
}
.border-#{name} {
border-color: ##name;
}
.bg-#{name} {
background: ##name;
}
// define colours
#blue: #0e9bd0;
#green: #009646;
#red: #f81010;
// import loop helper
#import "https://raw.githubusercontent.com/seven-phases-max/less.curious/master/src/for.less";
// define colour array
#colors: 'green', 'red', 'blue';
.for(#colors); .-each(#color) {
#name: e(#color);
.color-#{name} {
color: ##name;
}
.border-#{name} {
border-color: ##name;
}
.bg-#{name} {
background: ##name;
}
}

How to display custom video controls even in fullscreen

Update: Can't see to get things working in Firefox : (
How can I display custom video controls when the in fullscreen mode in modern browsers?
They disappear as soon as I go fullscreen. I'd like them to be available, and then I'll write some JavaScript to hide them on inactivity and show them once someone wiggles their mouse around.
HTML:
<video#video src="vid.mp4" preload poster="/images/poster.jpg">
<iframe src="https://youtube.com/embed/id" frameborder="0" allowfullscreen>
</video>
JS:
var bigPlayButton = document.getElementById('big-play-button')
var video = document.getElementById('video')
var playPauseButton = document.getElementById('play-pause')
var fullscreen = document.getElementById('fullscreen')
function toggleFullScreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen()
} else {
if (document.exitFullscreen) {
document.exitFullscreen()
}
}
}
fullscreen.addEventListener('click', function (event) {
if (!video.classList.contains('fullscreen')) {
video.requestFullscreen()
} else {
document.exitFullscreen()
}
}, false)
// Detect FullScreen changes and adjust button
document.addEventListener('fullscreenchange', function (event) {
if (document.fullscreenElement) {
fullscreen.children[0].src = '/images/nofullscreen.svg'
video.classList.add('fullscreen')
} else {
fullscreen.children[0].src = '/images/fullscreen.svg'
video.classList.remove('fullscreen')
}
}, false)
CSS
video::-webkit-media-controls {
display: none !important;
}
#custom-video-controls {
z-index: 2147483648;
}
I'm using this polyfill: https://github.com/neovov/Fullscreen-API-Polyfill
Edit
The significant change was targeting the parent tag: .vidFrame for fullscreen instead of the <video> tag as per Kaido's comment.
HTML5 video's controls need special handling if you want to override them. I'm assuming you want to do that since the controls already have the full screen feature built in the controls. This demo implements:
classList for toggling the button#fullScreen states of .on and .off and button#playPause states of .play and .pause.
:fullscreen pseudo-class to insure .vidBar is on the bottom when in full screen mode.
Shadow DOM CSS Styles that are needed to override the native player's controls.
Fullscreen API vendor specific methods to enter and exit full screen mode of course.
There's no volume slider, mute button, or scrubber, just the full screen button (button#fullScreen) and play button (button#playPause). If you want them, ask another question.
Details are commented in source.
It looks as if the Snippet isn't fully functional, so here's a functional Plunker. If that version cannot be reached, then review the embedded Plunker and click the full view button:
Demo
Note: SO sandbox has changed so this demo is not fully functional go to the links mentioned previously or copy and paste the demo on a text editor.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Full Screen Video Toggle</title>
<style>
.vidFrame { position: relative; top: 10%; width: 320px; height: auto; min-height: 180px; outline: 1px dashed red; }
.vidBar { position: absolute; bottom: 0; right: 0; left: 0; height: 40px; width: 99%; }
#fullScreen { position: absolute; bottom: 0; right: 0; width: 36px; height: 36px; outline: none; border: 1px solid transparent; border-radius: 6px; display: block; cursor: pointer; }
#fullScreen:hover { border: 1px groove #0ef; }
.on, .off { background: url('https://i.imgur.com/0FTwh6M.png') no-repeat; width: 36px; height: 36px; }
.off { background-position: 0 0 }
.on { background-position: -1px -50px }
#playPause { position: absolute; bottom: 0; left: 0; width: 36px; height: 36px; background: none; font-size: 36px; color: #0ff; line-height: 1; border: 1px solid transparent; display: block; cursor: pointer; outline: none; }
#playPause.play:before { content: '\25b6'; }
#playPause.pause:before { content: '\275a\275a'; }
.vid { position: absolute; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: auto; display: block; z-index: 1; outline: 1px dotted blue; }
/*
Fullscreen Pseudo-class:
https://developer.mozilla.org/en-US/docs/Web/CSS/:fullscreen
*/
.vidBar:-moz-full-screen { position: fixed; }
.vidBar:-webkit-full-screen { position: fixed; }
.vidBar:-ms-fullscreen { position: fixed; }
.vidBar:fullscreen { position: fixed; }
/*
Special Shadow DOM Settings to Override Default Controls:
https://css-tricks.com/custom-controls-in-html5-video-full-screen/
*/
video::-webkit-media-controls-enclosure { display:none !important; }
.vidBar { z-index: 2147483648; }
</style>
</head>
<body>
<figure class="vidFrame">
<video id="vid1" class="vid" src="http://techslides.com/demos/sample-videos/small.mp4"></video>
<figcaption class="vidBar">
<button id='playPause' class="play" title="Play/Pause Video"></button>
<button id='fullScreen' class="on" title="Enter/Exit Full Screen"></button>
</figcaption>
</figure>
<script>
/*
Toggle Button with classList:
https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
*/
var fullBtn = document.getElementById('fullScreen');
var playBtn = document.getElementById('playPause');
playBtn.addEventListener('click', function(event) {
var player = document.getElementById('vid1');
if(player.paused) {
playBtn.classList.remove('play');
playBtn.classList.add('pause');
player.play();
} else {
playBtn.classList.add('play');
playBtn.classList.remove('pause');
player.pause();
}
}, false);
fullBtn.addEventListener('click', function(event) {
var tgtEle = document.querySelector('.vidFrame');
var onOrOff = fullBtn.classList.contains('on');
if (onOrOff) {
enterFS(tgtEle);
fullBtn.classList.remove('on');
fullBtn.classList.add('off');
} else {
exitFS();
fullBtn.classList.add('on');
fullBtn.classList.remove('off');
}
}, false);
/*
Fullscreen API:
https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
*/
function enterFS(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
}
}
function exitFS() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
</script>
</body>
</html>
Use the Fullscreen API on the container element, not on the video
As #Kaiido says in the comments:
You have to call the enterFS method on the container element, not on
the video one.
So the answer is to use the Fullscreen API on the container element rather than the <video> element. This enables providing custom controls in that container which is now all in fullscreen.
For reference, that is the existing enterFS() function from the question:
function enterFS(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
}
}
I posted this answer because I had to read the page three times to figure out what was going on here.
There is great information in #zer00ne's answer that is relevant to others with similar issues, but it doesn't directly answer #Costa's original problem, which was previously only answered in a comment.

Sass variables with same names but different outcomes based on class?

I've tried to wrap my brain round this, I assume that I need an if statement somewhere along the way.
But I'd like to be able to do this with sass. But this will just take the green colour and ignore the default colour regardless of the class.
SASS
// Default Colours --------------------------------------------------------------
$textColor: #FFF;
.green {
// Base Colours --------------------------------------------------------------
$textColor: green;
}
body {
text: $textColor
}
HTML
<p>jamie</P> //Output is #FFF
<p class="green">jamie</P> //Output is green
Here is a little mixin you could use.
$base-color: green;
#mixin change-var($var: $base_color, $selector: x, $property: color) {
#if $selector == x {
$var: blue;
} #else if $selector == y {
$var: green
} #else {
$var: $var;
}
#{$property}: $var;
}
usage:
.x {
#include change-var($base-color, x, color)
}
.y {
#include change-var($base-color, y, background-color)
}
output:
.x {
color: blue;
}
.y {
background-color: green;
}
Try this
$textColor: #fff;
body {
color: $textColor;
}
.green {
$textColor: green;
color: $textColor;
}

CSS3 Webkit Full Screen Detection Not Working

I am having a really tuff time getting CSS3 to detect full screen. Right now, I have:
:-webkit-full-screen body {
color: red;
background: red;
}
When hitting F11 in my browser, nothing turns red.
For testing, I am trying to turn everything red but not having success. I am using Chromium 31.0.1650.57. Am I using :-webkit-full-screen incorrectly?
I think this has something to do with you pressing F11 to get fullscreen. You need to trigger the fullscreen via webkitRequestFullscreen and the other cross-browser versions of this. Also, I think that the CSS doesn't apply to the body.
Try to use a wrapper and apply it to that element:
document.getElementById('gofullscreen').addEventListener('click', function() {
var elem = document.getElementsByTagName("body")[0];
elem.webkitRequestFullscreen();
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
}
});
html,
body {
width: 100%;
height: 100%;
}
#wrapper {
height: 100%;
width: 100%;
}
:-webkit-full-screen #wrapper {
color: red;
background: red;
}
<div id="wrapper">
fullscreen
</div>
See Fiddle and Fullscreen version
(Use the Fiddle link to see the code and the Fullscreen version to see it working, Fiddle doesn't allow fullscreen I think).
But the :-webkit-full-screen and the like are experimental, so don't rely on it.
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode
Making an element go full-screen
Like this on some browser it may work:
function gofullscreen() {
var elem = document.getElementById("VideoWrapper");
elem.webkitRequestFullscreen();
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
}
}
$("#buttonGo").click(function(){gofullscreen()});
**//CSS**
:-webkit-full-screen #VideoWrapper {
color: red;
background: red;
width: 100%;
height: 100%;
}
:-moz-full-screen #VideoWrapper {
color: red;
background: red;
width: 100%;
height: 100%;
}
:-ms-fullscreen #VideoWrapper {
color: red;
background: red;
width: 100%;
height: 100%;
}
:full-screen #VideoWrapper {
color: red;
background: red;
width: 100%;
height: 100%;
}
:fullscreen #VideoWrapper {
color: red;
background: red;
width: 100%;
height: 100%;
}
On some browser it might not work
To make it work across regardless of standard Css
$("#buttonGo").click(function(){
$("#VideoWrapper").css({height: '100%',width:'100%',background:'red',color:'red'});
gofullscreen()});
This work well on chrome , ff , ms

How can I build a flexible ratings widget in LESS instead of straight CSS?

I found this flexible, one-image rating widget built with SASS by AirBnB. I'd like to create something similar with LESS. I know I could just write out the CSS, but I'd like to do it dynamically with LESS. My main problem seems to be dynamically adding the counter _1 ... _10 to the class (.filled_1 ... filled_10). Is this possible in LESS?
Here's the working SASS code:
$starWidth: 44px;
$starOffset: 0 -43px;
$numStars: 5;
$steps: 2;
$total: $numStars * $steps;
#mixin filled($n: 0) {
width: ($starWidth / $steps) * $n;
}
.stars {
background: url(/images/sprite.png) repeat-x top left;
height: 43px;
&.empty {
background-position: $starOffset;
width: $numStars * $starWidth;
}
#for $i from 0 through ($total) {
&.filled_#{$i} { #include filled($i) }
}
}
This turns out code like this in CSS:
.stars {
background: url(/images/sprite.png) repeat-x top left;
height: 43px; }
.stars.empty {
background-position: 0 -43px;
width: 220px; }
.stars.filled_0 {
width: 0px; }
.stars.filled_1 {
width: 22px; }
.stars.filled_2 {
width: 44px; }
.stars.filled_3 {
width: 66px; }
.stars.filled_4 {
width: 88px; }
.stars.filled_5 {
width: 110px; }
.stars.filled_5 {
width: 132px; }
.stars.filled_7 {
width: 154px; }
.stars.filled_8 {
width: 176px; }
.stars.filled_9 {
width: 198px; }
.stars.filled_10 {
width: 220px; }
How can I do the same loop and include in LESS instead of CSS?
The final HTML will look like this: (where 9 will show 4.5 stars)
<div class="stars empty">
<div class="stars filled_9">4.5</div>
</div>
As simple as link to a resource: http://blog.thehippo.de/2012/04/programming/do-a-loop-with-less-css
And here is code sample from the resource:
LESS code:
#iterations: 30;
// helper class, will never show up in resulting css
// will be called as long the index is above 0
.loopingClass (#index) when (#index > 0) {
// create the actual css selector, example will result in
// .myclass_30, .myclass_28, .... , .myclass_1
(~".myclass_#{index}") {
// your resulting css
my-property: -#index px;
}
// next iteration
.loopingClass(#index - 1);
}
// end the loop when index is 0
.loopingClass (0) {}
// "call" the loopingClass the first time with highest value
.loopingClass (#iterations);
Resulting CSS:
.myclass_30 {
my-property: -30 px;
}
.myclass_29 {
my-property: -29 px;
}
.......
.......
.......
.myclass_1 {
my-property: .1 px;
}
Stackoverflow user GnrlBzik shared a looping strategy for LESS, but the result looked more complex than I had hoped. Here is a working solution that still looks elegant for those that will have a static amount of stars.
#starWidth: 44px;
#starOffset: 0 -43px;
#numStars: 5;
.starCount(#starSpan: 1) {
width: (#starWidth / 2) * #starSpan;
}
.stars {
background: url('/images/sprites/stars.png') repeat-x top left;
height: 43px;
display:block;
&.empty {
background-position: #starOffset;
width: (#starWidth * #numStars);
}
&.filled_0 { .starCount(0); }
&.filled_1 { .starCount(1); }
&.filled_2 { .starCount(2); }
&.filled_3 { .starCount(3); }
&.filled_4 { .starCount(4); }
&.filled_5 { .starCount(5); }
&.filled_6 { .starCount(6); }
&.filled_7 { .starCount(7); }
&.filled_8 { .starCount(8); }
&.filled_9 { .starCount(9); }
&.filled_10 { .starCount(10); }
}