knockoutjs:Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding
- <!DOCTYPE html>
- <html lang="ja">
- <head>
- <meta charset="UTF-8">
- <title>knockoutjs:Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding</title>
- <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
- <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
- </head>
- <body>
- <h1>Example 3: JqueryのslideUp/slideDownを組み込む:初期値設定:The custom binding</h1>
- <div data-bind="slideVisible: giftWrap, slideDuration:600">You have selected the option</div>
- <label><input type="checkbox" data-bind="checked: giftWrap" /> Gift wrap</label>
- <script type="text/javascript">
- ko.bindingHandlers.slideVisible = {
- init: function(element, valueAccessor) {
- var value = ko.utils.unwrapObservable(valueAccessor()); // Get the current value of the current property we're bound to
- $(element).toggle(value); // jQuery will hide/show the element depending on whether "value" or true or false
- },
- update: function(element, valueAccessor, allBindingsAccessor)
- {
- // First get the latest data that we're bound to
- var value = valueAccessor(), allBindings = allBindingsAccessor();
- // Next, whether or not the supplied model property is observable, get its current value
- var valueUnwrapped = ko.utils.unwrapObservable(value);
- // Grab some more data from another binding property
- var duration = allBindings.slideDuration || 400; // 400ms is default duration unless otherwise specified
- // Now manipulate the DOM element
- if (valueUnwrapped == true)
- {
- $(element).slideDown(duration); // Make the element visible
- }
- else
- {
- $(element).slideUp(duration); // Make the element invisible
- }
- }
- };
- var viewModel = {
- giftWrap: ko.observable()
- };
- ko.applyBindings(viewModel);
- </script>
- <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
- <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
- <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
- </div>
- <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
- Copyright © 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
- </div>
- </body>
- </html>