在表单内使用Javascript字体选择

时间:2015-02-15 13:04:34

标签: javascript jquery html css angularjs

我目前正在使用此脚本选择字体样式,如何包含name="font_style"id="font_style"以允许将所选字体传递到表单然后提交到数据库

这是脚本:



<!DOCTYPE html>
<html>

<head>

  <meta charset="UTF-8">

  <!--
Copyright (c) 2015 by VISIONCAN (http://codepen.io/visioncan/pen/xLijC)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

  <title>CodePen - font-select dropdown</title>

  <style>
  body {
  background: #eee;
}

.font-dropdown {
  width: 220px;
  border-radius: 5px;
  position: relative;
  margin: 10px auto 0;
  background-color: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  outline: none;
}
.font-dropdown .label {
  -moz-user-select: -moz-none;
  -ms-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  font-size: 1.2em;
  position: relative;
  display: block;
  padding: 0 10px;
  height: 36px;
  line-height: 36px;
  z-index: 2;
  white-space: nowrap;
  overflow: hidden;
}
.font-dropdown .label:after {
  -moz-transition: -moz-transform 0.3s;
  -o-transition: -o-transform 0.3s;
  -webkit-transition: -webkit-transform 0.3s;
  transition: transform 0.3s;
  position: absolute;
  width: 0;
  height: 0;
  content: "";
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid #ccc;
  left: auto;
  right: 11px;
  bottom: 13px;
}
.font-dropdown .dropdown-list {
  -moz-transition: all 0.2s;
  -o-transition: all 0.2s;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  list-style: none;
  margin: 0;
  padding: 0;
  width: 220px;
  border-radius: 5px;
  max-height: 66.66667px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.15);
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
  border: 1px solid #ccc;
  background-color: #fff;
  overflow-x: hidden;
  overflow-y: auto;
  opacity: 0;
  visibility: hidden;
}
.font-dropdown.active .dropdown-list {
  max-height: 200px;
  top: 37px;
  opacity: 1;
  visibility: visible;
}
.font-dropdown.active .label:after {
  -moz-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  -webkit-transform: rotate(180deg);
  transform: rotate(180deg);
}
.font-dropdown li {
  padding: 0 10px;
  font-size: 1.4em;
  line-height: 1.5em;
  white-space: nowrap;
  overflow: hidden;
}
.font-dropdown li:hover {
  color: #fff;
  background-color: #2689d6;
}
.font-dropdown .sel {
  background-color: #f0f0f0;
  color: #2689d6;
}

  </style>

  <script>
    window.console = window.console || function(t) {};
    window.open = function(){ console.log('window.open is disabled.'); };
    window.print = function(){ console.log('window.print is disabled.'); };
    // Support hover state for mobile.
    if (false) {
      window.ontouchstart = function(){};
    }
  </script>

</head>

<body>

  <div ng-app="fdApp">
  <div class="font-dropdown" tabindex="1" fd-font-dropdown>
    <span class="label" ng-style="{{'fontslist[selectedIdx].style'}}">{{fontslist[selectedIdx].name}}</span>
  <ul class="dropdown-list">
      <li ng-repeat="font in fontslist" ng-click="changeFont($index)" ng-class="{'sel': $index == selectedIdx}" ng-style="{{font.style}}">
          {{font.name}}
      </li>
  </ul>
  </div>
</div>

  <script src='http://code.angularjs.org/1.2.0-rc.3/angular.min.js'></script>

  <script>
    if (document.location.search.match(/type=embed/gi)) {
      window.parent.postMessage('resize', "*");
    }
  </script>

  <script src="//assets.codepen.io/assets/common/stopExecutionOnTimeout-6c99970ade81e43be51fa877be0f7600.js"></script>

  <script>
    (function () {
    var app;
    window.WebFontConfig = { google: { families: [] } };
    app = angular.module('fdApp', []);
    app.controller('FontDropdownCtrl', function ($scope) {
        var WEBFONTAPI;
        WEBFONTAPI = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
        this.FONTSLIST = [
            {
                name: 'Source Sans Pro',
                face: 'Source+Sans+Pro:900italic',
                style: {
                    fontFamily: 'Source Sans Pro',
                    fontWeight: 900,
                    fontStyle: 'italic'
                }
            },
            {
                name: 'Quattrocento Sans',
                face: 'Quattrocento+Sans',
                style: { fontFamily: 'Quattrocento Sans' }
            },
            {
                name: 'Ubuntu',
                face: 'Ubuntu:700',
                style: { fontFamily: 'Ubuntu' }
            },
            {
                name: 'Arizonia',
                face: 'Arizonia',
                style: { fontFamily: 'Arizonia' }
            },
            {
                name: 'Lora',
                face: 'Lora:700',
                style: {
                    fontFamily: 'Lora',
                    fontWeight: 700
                }
            },
            {
                name: 'Sansita One',
                face: 'Sansita+One',
                style: { fontFamily: 'Sansita One' }
            },
            {
                name: 'Armata',
                face: 'Armata',
                style: { fontFamily: 'Armata' }
            },
            {
                name: 'Black Ops One',
                face: 'Black+Ops+One',
                style: { fontFamily: 'Black Ops One' }
            },
            {
                name: 'Russo One',
                face: 'Russo+One',
                style: { fontFamily: 'Russo One' }
            }
        ];
        return this.loadFonts = function () {
            var font, s, wf, _i, _len, _ref;
            _ref = this.FONTSLIST;
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                font = _ref[_i];
                WebFontConfig.google.families.push(font.face);
            }
            wf = document.createElement('script');
            wf.src = ('https:' === document.location.protocol ? 'https:' : 'http:') + WEBFONTAPI;
            wf.type = 'text/javascript';
            wf.async = 'true';
            s = document.getElementsByTagName('script')[0];
            return s.parentNode.insertBefore(wf, s);
        };
    });
    app.directive('fdFontDropdown', function () {
        return {
            restrict: 'A',
            controller: 'FontDropdownCtrl',
            link: function (scope, element, attr, Ctrl) {
                Ctrl.loadFonts();
                scope.fontslist = Ctrl.FONTSLIST;
                scope.selectedIdx = Math.floor(Math.random() * scope.fontslist.length);
                scope.changeFont = function (idx) {
                    scope.selectedIdx = idx;
                    return console.log(idx);
                };
                return element.bind('click', function () {
                    return element.toggleClass('active');
                });
            }
        };
    });
}.call(this));
    //@ sourceURL=pen.js
  </script>

</body>

</html>
&#13;
&#13;
&#13;

如果我做错了什么或者我应该添加什么,请你告诉我吗? 非常感谢

1 个答案:

答案 0 :(得分:0)

fd-font-dropdown - div中,您应该包含一个<input /> - 元素,该元素将保留value属性中的所选字体。

changeFont - 函数中,您已经设置了selectedIdx值,所以这应该可以解决问题:

<input type="hidden" name="font" value="{{selectedIdx}}" />

如果您将整个fd-font-dropdown - dev放在表单中,font - 输入的值将随之发送。

所以,它看起来像这样:

<form action="..." method="...">
    <div ng-app="fdApp">
        <div class="font-dropdown" tabindex="1" fd-font-dropdown>
            <input type="hidden" name="font" value="{{selectedIdx}}" />
            <span class="label" ng-style="{{'fontslist[selectedIdx].style'}}">{{fontslist[selectedIdx].name}}</span>
            <ul class="dropdown-list">
                <li ng-repeat="font in fontslist" ng-click="changeFont($index)" ng-class="{'sel': $index == selectedIdx}" ng-style="{{font.style}}">
                    {{font.name}}
                </li>
            </ul>
        </div>
    </div>
    <input type="submit" value="Submit the form" />
</form>