如何添加预览或选择HTML字体动态地在谷歌应用程序脚本的HTML文件字体大小和字体类型

A B C D

我在前端开发新手。我想创建一个字体选择一种形式,这将需要输入文本,字体类型和大小的用户,并相应地改变输入文本。这是显示用户所选字体的预览。下面是我尝试过的东西,

<!DOCTYPE html>
<html>
<style>
@import url('https://fonts.googleapis.com/css?family=Open+Sans|Oswald|Anton|Kaushan+Script|Rochester|Sacramento');

.openSans {font-family: 'Open Sans', sans-serif;}
.oswald {font-family: 'Oswald', sans-serif;}
.anton {font-family: 'Anton', sans-serif;}
.kaushanScript {font-family: 'Kaushan Script', cursive;}
.rochester {font-family: 'Rochester', sans-serif;}
.sacramento {font-family: 'Sacramento', sans-serif;}

.uppercase-text {text-transform: uppercase;}
.lowercase-text {text-transform: lowercase;}
.capitalize-text {text-transform: capitalize;}


body {font-family: 'Open Sans', sans-serif;}
.wrapper {padding: 10px;}
.fontsSelectBox-button {margin-top: 10px; }
select {width: 200px;}
fieldset {
    border: 0;
}

label {
    display: block;
    margin: 10px 0 0 0;
}

input {
  font-family: 'Open Sans', sans-serif;
  padding: 0;
  font-size: 24px;
  border: 0;
  width: 100%;
}

.fontForm {
    max-width: 600px;
}

.checkbox-label {
    display: inline-block;
    
}
    
.grid {
  margin: -10px;
  padding: 10px;
  letter-spacing: -0.286em;
  position: relative;   
}

.grid-cell {
  display: inline-block;
  padding: 10px;
  margin: ;
  list-style: none;
  vertical-align: top;
  width: 100%;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  letter-spacing: 0;
  direction: ltr;
}

.size1of1 {
    width: 100%;
}

.size1of2 {
    width: 50%;
}

.container {
  position: relative;
}

.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
}
.image 
{ 
position: relative; /* To help the image + text element to get along with the rest of the page*/ 
width: auto; /* for IE 6 */ 
} 
h2 
{ 
position: absolute; /* To place the text on the image*/
top: 200px; /* The exact location of the text from the top of the image*/
left: 0; /* Other beautification stuff */
width: 100%; 
}
/* Coloring time */
h2 span /* decorating the text within the span tag */
{ 
color:; 
font: ; 
letter-spacing: -1px; 
padding: 10px; 
}
h2 span.spacer { padding:0 5px; } /* to pad the background color of text to make it look more elegant */

 
</style>
<base target="_top">

    <head>
    <script type="text/javascript">
      
          
    function myfunction(){
    (function($){
    var fontTxtField = $("#fontTxtField");
    var fontsSelectBox = $("#fontsSelectBox");

    $(fontsSelectBox).selectmenu({
        change : function(event, data) {
            fontTxtField.removeClass("openSans").removeClass("oswald")
            .removeClass("anton").removeClass("kaushanScript")
            .removeClass("rochester").removeClass("sacramento")
            .addClass(data.item.value);
            console.log(data)
        },
        icons: { button: "ui-icon-circle-triangle-s" }
    });
  
  var fontStyle;
    if (document.getElementById("uppercase").checked){
     fontStyle = document.getElementById("uppercase").value;
    }else if(document.getElementById("lowercase").checked){
    fontStyle = document.getElementById("lowercase").value;
    }else if(document.getElementById("capitalize").checked){
    fontStyle = document.getElementById("capitalize").value;
    }else{
    fontStyle = document.getElementById("normal").value;
    }
    $(fontStyle).change(function () {
        var value = this.value;
        switch(value) {
            case "uppercase":
                fontTxtField.addClass("uppercase-text").removeClass("lowercase-text").removeClass("capitalize-text");
            break;
            case "lowercase":
                fontTxtField.addClass("lowercase-text").removeClass("uppercase-text").removeClass("capitalize-text");
            break;
            case "capitalize":
                fontTxtField.addClass("capitalize-text").removeClass("uppercase-text").removeClass("lowercase-text");
            break;          
            case "normal":
            default:
                fontTxtField.removeClass("uppercase-text").removeClass("lowercase-text").removeClass("capitalize-text");;
            break;
        };
    });
   var inputFont = fontTxtField[0]['value']
   var si = fontsSelectBox[0]['options']['selectedIndex']
   var selectedFont = fontsSelectBox[0]['options'][si]['text']
})(jQuery);
}
    </script>
        <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css">
        <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <link href="style.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
        <script src="script.js"></script>
    </head>
<body>
    <div class="wrapper">    
        <h1>Font Selector</h1>
        <fieldset class="fontForm grid">
    <div class="grid-cell">
            <div class="grid-cell size1of2">
    <div class="grid-cell size1of2">
                <label for="fontsSelectBox">Choose Font</label>
                <select id="fontsSelectBox" name="fontsSelectBox">
                <option value="openSans">Open Sans</option>
                <option value="oswald">Oswald</option>
                <option value="anton">Anton</option>
                <option value="kaushanScript">Kaushan Script</option>
                <option value="rochester">Rochester</option>
                <option value="sacramento">Sacramento</option>  
                </select>
                </div>
            </div>
            
            <div class="grid-cell size1of1">
                <label for="uppercase" class="checkbox-label"><input type="radio" name="fontStyle" value="uppercase" id="uppercase"> Uppercase</label>
                <label for="lowercase" class="checkbox-label"><input type="radio" name="fontStyle" value="lowercase" id="lowercase"> Lowercase</label>
                <label for="capitalize" class="checkbox-label"><input type="radio" name="fontStyle" value="capitalize" id="capitalize"> Capitalize</label>
                <label for="normal" class="checkbox-label"><input type="radio" name="fontStyle" value="normal" id="normal" checked> Normal</label>
            </div>
        </fieldset>
    <div class="container">
      <div class="image"> <!-- the image container -->
    <img src="https://static.wixstatic.com/media/776cbf_6940447e8d13424b9957749730292300~mv2.png/v1/fill/w_404,h_750,al_c,q_90,usm_0.66_1.00_0.01/776cbf_6940447e8d13424b9957749730292300~mv2.webp" alt="" /> <!-- the image -->
    <h2>
    <span><div class="grid-cell image">
            <input type="text" name="fontTxtField" id="fontTxtField" placeholder="Your text here"/><span class='spacer'></span>
    </h2> 
      <div class="center">
      <p><button onclick="myfunction()">Submit</button></p>
    </div>
    </div>
        </div>
    </div>          
</body>
</html>

在上面的代码中,我能够改变字体类型提交按钮,这是我不想和不能改变字体大小或类型后(大写,小写..)。我哪里做错了还是什么,我在这里失踪?

杨布里科斯

如果我理解正确的话,字体和类型正在发生变化,只有当表单提交,并希望他们更新所选的选项更改时。如果是这种情况,您可以改用更改事件

例如,在select使用:

<select id="fontsSelectBox" name="fontsSelectBox" onchange="myfunction()">

对于radio输入,你必须在添加onchange事件到每个单输入,所以你可能会更好的使用事件侦听器来代替:

var rad = document.getElementsByName("fontStyle");
for (var i = 0; i < rad.length; i++) {
    rad[i].addEventListener('change', myfunction);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何向fyne UI应用程序添加填充,字体大小和颜色

在Vaadin Flow Web应用程序中动态更改字体,字体大小,字体颜色等

用户选择大小后,应用程序中的字体大小会动态更改(Xamarin)

应用程序字体大小不变

更改整个应用程序的比例和字体大小

如何增加整个应用程序的字体大小?

通过用户选择增加和减少android应用程序的字体大小

如何从Java文件更改字体大小和字体颜色?

如何获取HTML中的字体大小

如何获取HTML中的字体大小?

R Markdown-在html输出中更改字体大小和字体类型

如何通过更改设置字体大小来使应用程序中的字体大小不受影响

Java字体大小与HTML字体大小

在整个应用程序中更改字体大小

更改移动Web应用程序的字体大小选项?

增加Gtk应用程序的字体大小

限制支持的动态类型字体大小

如何随着字体大小变化动态平滑地增加和减少UILabel的文本

随着键入更多类型,使 HTML 输入字体大小缩小

用于网站和Web应用程序的最佳字体大小em或px?

python-docx中的字体类型和字体大小

如何防止系统字体大小更改对android应用程序的影响?

Ionic 3应用程序-如何使字体大小独立于本机设置?

如何使用UISLIDER控制iOS应用程序的所有UI控件的字体大小?

如何在正在运行的Gtk3应用程序中修改字体大小?

如何在运行Gnome的Ubuntu 18.04中增加特定应用程序的字体大小?

如何防止系统字体大小更改对nativescript-angular android应用程序的影响

如何在 Flutter 中为文本元素设置应用程序宽字体大小?

从本机设置更改字体大小后,Android应用程序中TextView的字体大小也会更改