All Components

MVVM / Style binding

One Big Orange Spot.

View source code:

<div id="target" data-bind="style: {
        backgroundColor: selectedBackground,
        color: selectedColor,
        borderStyle: selectedBorderStyle,
        borderColor: selectedBorderColor,
        webkitBorderRadius: selectedBorderRadius,
        mozBorderRadius: selectedBorderRadius,
        borderRadius: selectedBorderRadius
    }">
    One Big Orange Spot.
</div>
<ul>
    <li>
        Color: <select data-text-field="name" data-value-field="hex" data-bind="source: colors, value: selectedColor"></select>
    </li>
    <li>
        <label>Background color: <select data-text-field="name"
            data-value-field="hex" data-bind="source: colors, value: selectedBackground"></select></label>
    </li>
    <li>
        <label>Border color: <select data-text-field="name"
            data-value-field="hex" data-bind="source: colors, value: selectedBorderColor"></select></label>
    </li>
    <li>
        <label>Border style: <select data-text-field="name"
            data-value-field="value" data-bind="source: borders, value: selectedBorderStyle"></select></label>
    </li>
    <li>
        <label>Border radius: <select data-bind="source: radii, value: selectedBorderRadius"></select></label>
    </li>
</ul>
        

View model source code:

var viewModel = kendo.observable({
    selectedColor: "#8b0000",
    selectedBorderRadius: "20px",
    selectedBackground: "#ffd700",
    selectedBorderStyle: "solid",
    selectedBorderColor: "#ff8c00",
    radii: [ "0px", "5px", "10px", "20px" ],
    colors: [
        { name: "Dark Red", hex: "#8b0000" },
        { name: "Orange Red", hex: "#ff4500" },
        { name: "Dark Orange", hex: "#ff8c00" },
        { name: "Gold", hex: "#ffd700" }
    ],
    borders: [
        { name: "Solid", value: "solid" },
        { name: "Dashed", value: "dashed" },
        { name: "Double", value: "double" },
        { name: "Dotted", value: "dotted" },
        { name: "Ridge", value: "ridge" },
        { name: "None", value: "none" }
    ]
});

kendo.bind($("#example"), viewModel);
        

HTML5/JavaScript 源码

<!DOCTYPE html>
<html>
 <head> 
  <title>Style binding</title> 
  <meta charset="utf-8"> 
  <link href="../content/shared/styles/examples-offline.css" rel="stylesheet"> 
  <link href="../../styles/kendo.common.min.css" rel="stylesheet"> 
  <link href="../../styles/kendo.rtl.min.css" rel="stylesheet"> 
  <link href="../../styles/kendo.default.min.css" rel="stylesheet"> 
  <link href="../../styles/kendo.dataviz.min.css" rel="stylesheet"> 
  <link href="../../styles/kendo.dataviz.default.min.css" rel="stylesheet"> 
  <script src="../../js/jquery.min.js"></script> 
  <script src="../../js/kendo.all.min.js"></script> 
  <script src="../content/shared/js/console.js"></script> 
  <script>
        
    </script> 
 </head> 
 <body> 
  <a class="offline-button" href="../index.html">Back</a> 
  <div id="example"> 
   <div class="demo-section k-content"> 
    <div id="target" data-bind="style: {
                backgroundColor: selectedBackground,
                color: selectedColor,
                borderStyle: selectedBorderStyle,
                borderColor: selectedBorderColor,
                webkitBorderRadius: selectedBorderRadius,
                mozBorderRadius: selectedBorderRadius,
                borderRadius: selectedBorderRadius
            }">
      One Big Orange Spot. 
    </div> 
    <ul class="fieldlist"> 
     <li> <label>Color:</label> <select data-text-field="name" data-value-field="hex" data-bind="source: colors, value: selectedColor"></select> </li> 
     <li> <label>Background color:</label> <select data-text-field="name" data-value-field="hex" data-bind="source: colors, value: selectedBackground"></select> </li> 
     <li> <label>Border color:</label> <select data-text-field="name" data-value-field="hex" data-bind="source: colors, value: selectedBorderColor"></select> </li> 
     <li> <label>Border style:</label> <select data-text-field="name" data-value-field="value" data-bind="source: borders, value: selectedBorderStyle"></select> </li> 
     <li> <label>Border radius:</label> <select data-bind="source: radii, value: selectedBorderRadius"></select> </li> 
    </ul> 
   </div> 
   <script>
        $(document).ready(function() {
            var viewModel = kendo.observable({
                selectedColor: "#8b0000",
                selectedBorderRadius: "20px",
                selectedBackground: "#ffd700",
                selectedBorderStyle: "solid",
                selectedBorderColor: "#ff8c00",
                radii: [
                    "0px",
                    "5px",
                    "10px",
                    "20px"
                ],
                colors: [
                    { name: "Dark Red", hex: "#8b0000" },
                    { name: "Orange Red", hex: "#ff4500" },
                    { name: "Dark Orange", hex: "#ff8c00" },
                    { name: "Gold", hex: "#ffd700" }
                ],
                borders: [
                    { name: "Solid", value: "solid" },
                    { name: "Dashed", value: "dashed" },
                    { name: "Double", value: "double" },
                    { name: "Dotted", value: "dotted" },
                    { name: "Ridge", value: "ridge" },
                    { name: "None", value: "none" }
                ]
            });

            kendo.bind($("#example"), viewModel);
        });
    </script> 
   <div class="box wide"> 
    <h4>View source code:</h4> 
    <pre class="prettyprint">
<div id="target" data-bind="style: {
        backgroundColor: selectedBackground,
        color: selectedColor,
        borderStyle: selectedBorderStyle,
        borderColor: selectedBorderColor,
        webkitBorderRadius: selectedBorderRadius,
        mozBorderRadius: selectedBorderRadius,
        borderRadius: selectedBorderRadius
    }">
    One Big Orange Spot.
</div>
<ul>
    <li>
        Color: <select data-text-field="name" data-value-field="hex" data-bind="source: colors, value: selectedColor"></select>
    </li>
    <li>
        <label>Background color: <select data-text-field="name"
            data-value-field="hex" data-bind="source: colors, value: selectedBackground"></select></label>
    </li>
    <li>
        <label>Border color: <select data-text-field="name"
            data-value-field="hex" data-bind="source: colors, value: selectedBorderColor"></select></label>
    </li>
    <li>
        <label>Border style: <select data-text-field="name"
            data-value-field="value" data-bind="source: borders, value: selectedBorderStyle"></select></label>
    </li>
    <li>
        <label>Border radius: <select data-bind="source: radii, value: selectedBorderRadius"></select></label>
    </li>
</ul>
        </pre> 
    <h4 style="padding-top: 2em;">View model source code:</h4> 
    <pre class="prettyprint">
var viewModel = kendo.observable({
    selectedColor: "#8b0000",
    selectedBorderRadius: "20px",
    selectedBackground: "#ffd700",
    selectedBorderStyle: "solid",
    selectedBorderColor: "#ff8c00",
    radii: [ "0px", "5px", "10px", "20px" ],
    colors: [
        { name: "Dark Red", hex: "#8b0000" },
        { name: "Orange Red", hex: "#ff4500" },
        { name: "Dark Orange", hex: "#ff8c00" },
        { name: "Gold", hex: "#ffd700" }
    ],
    borders: [
        { name: "Solid", value: "solid" },
        { name: "Dashed", value: "dashed" },
        { name: "Double", value: "double" },
        { name: "Dotted", value: "dotted" },
        { name: "Ridge", value: "ridge" },
        { name: "None", value: "none" }
    ]
});

kendo.bind($("#example"), viewModel);
        </pre> 
   </div> 
   <style>
        .demo-section label {
            display: block;
            float: left;
            clear: left;
            width: 120px;
            margin: .5em 0;
        }

        .demo-section select {
            float: left;
            width: 120px;
            margin: .5em 0;
        }

        #target {
            width: 100%;
            border-width: 4px;
            padding: 2em;
            display: block;
            box-sizing: border-box;
            margin: 0 auto 30px;
            -webkit-transition: all 500ms;
            -moz-transition: all 500ms;
            transition: all 500ms;
            text-align: center;
            vertical-align: middle;
        }

        .prettyprint {
            background-color: #fff;
            border: 1px solid #ccc;
            overflow: auto;
            padding: 5px;
        }
    </style> 
  </div>   
 </body>
</html>