Posted in ideas
12445
8:55 am, October 6, 2021
 

Idea: Google Font Dropdown Selector Code

just an idea at the moment, a dropdown list of google fonts and when selected it loads that selected font using the @import css tag. 

Loading the fonts Gist : not tested

https://gist.github.com/PeterBooker/7199234 

Using the google font API

https://stackoverflow.com/questions/36872550/google-web-fonts-in-a-dropdown-list-dynamically-with-respective-font-weights 

PHP

<?php
/* 
 * Google Font Chooser
 */

$path = "/home/kebopowe/public_html/functions/fonts.txt";
//$path = "http://kebopowered.com/functions/fonts.txt"; // use this if remote

$request = file_get_contents( $path );

$fonts = json_decode( $request );

?>

<!DOCTYPE html>
<html>
    
    <head>
        
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
            
        <style type="text/css">
            select {
                border: 1px solid #ddd;
                font-size: 18px;
                height: 12em;
            }
            select optgroup {
            }
            select option {
                padding: 2px 6px;
            }
        </style>
            
    </head>
    
    <body>
        
        <h2>Google Webfonts Selector</h2>
        
        <select id="font-selector" style="font-family: '<?php echo $fonts->items[0]->family; ?>', Arial,​ sans-serif;" size="4" multiple="multiple">
            
            <?php foreach ( $fonts->items as $font ) { ?>
            
            <optgroup style="font-family: '<?php echo $font->family; ?>', Arial,​ sans-serif;" data-src="http://fonts.googleapis.com/css?family=<?php echo str_replace(' ', '+', $font->family); ?>&text=<?php echo str_replace(' ', '+', $font->family); ?>">
                <option value="<?php echo str_replace(' ', '+', $font->family); ?>"><?php echo $font->family; ?></option>
            </optgroup>
            
            <?php } ?>
            
        </select>
        
        <script>
            $( document ).ready(function() {
                
                setTimeout( function() {
                
                    $.each( $("#font-selector optgroup"), function() {

                        var src = $(this).data( "src" );
                        $('head').append("<link href='" + src + "' rel='stylesheet' type='text/css'>");

                    });
                
                }, 0);
                
                $("#font-selector").change(function() {
                    var selected = $("#font-selector option:selected").text();
                    $(this).css( 'font-family', selected );
                });
                
            });
        </script>
        
    </body>
    
</html>

View Statistics
This Week
295
This Month
1599
This Year
3589

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

backface-visibility

The backface-visibility CSS property sets whether the back face of an element is visible when turned towards the user.
backface-visibility css reference