{"id":3806,"date":"2014-03-11T14:24:25","date_gmt":"2014-03-11T21:24:25","guid":{"rendered":"http:\/\/blog.cloudfour.com\/?p=3806"},"modified":"2023-02-15T14:31:13","modified_gmt":"2023-02-15T22:31:13","slug":"simpleslideview-controlling-flow-with-javascript","status":"publish","type":"post","link":"https:\/\/cloudfour.com\/thinks\/simpleslideview-controlling-flow-with-javascript\/","title":{"rendered":"SimpleSlideView: Controlling Flow With JavaScript"},"content":{"rendered":"<p>In July we released <a href=\"https:\/\/cloudfour.com\/thinks\/simpleslideview\/\">SimpleSlideView<\/a>, a jQuery and Zepto plugin for simple, responsive sliding views (<a href=\"http:\/\/cloudfour.github.io\/SimpleSlideView\/\">here&#8217;s a demo<\/a>). In the last few months, I&#8217;ve noticed an uptick in developers emailing to ask a variation of this question:<\/p>\n<blockquote><p>\n  How do I change or stop a view transition based on user input?\n<\/p><\/blockquote>\n<p>It&#8217;s a great question, and one that may not be immediately apparent from the demo alone.<\/p>\n<p>When using SimpleSlideView, there are two ways to navigate between views. The simplest way is <a href=\"https:\/\/github.com\/cloudfour\/SimpleSlideView#with-html\">with HTML<\/a>, which is great for predictable, non-dynamic interactions or prototyping:<\/p>\n<pre aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">data-pushview<\/span>=<span class=\"hljs-string\">\"#result\"<\/span>&gt;<\/span>See Result<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The straightforwardness of this technique is a double-edged sword. It&#8217;s easy to use, but it&#8217;s also kind of static. What if we want to change the destination? Or prevent the transition altogether? For that, we&#8217;ll want to use <a href=\"https:\/\/github.com\/cloudfour\/SimpleSlideView#with-javascript\">JavaScript instead<\/a>.<\/p>\n<p>We can remove the <code>data-pushview<\/code> attribute from the button, but we&#8217;ll need some way to select it via JS, so let&#8217;s give it an ID:<\/p>\n<pre aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"result-btn\"<\/span>&gt;<\/span>See Result<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>And here&#8217;s the scripty part:<\/p>\n<pre aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/\/ Save the simpleSlideView instance to a variable<\/span>\n<span class=\"hljs-keyword\">var<\/span> slideView = $(<span class=\"hljs-string\">'#container'<\/span>).simpleSlideView();\n<span class=\"hljs-comment\">\/\/ Add a click event handler to the button<\/span>\n$(<span class=\"hljs-string\">'#result-btn'<\/span>).click(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n  <span class=\"hljs-comment\">\/\/ On click, push the view with an ID of 'result'<\/span>\n  slideView.pushView(<span class=\"hljs-string\">'#result'<\/span>);\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Right now this probably feels like a step backward. It&#8217;s more verbose. But we&#8217;ve gained the freedom to define the sliding-view behavior any way we like.<\/p>\n<p>Suppose you only want to transition when certain criteria are met:<\/p>\n<pre aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> (isValid) {\n  slideView.pushView(<span class=\"hljs-string\">'#thanks'<\/span>);\n} <span class=\"hljs-keyword\">else<\/span> {\n  alert(<span class=\"hljs-string\">'Oops! Something seems off.'<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Or maybe you&#8217;d like the next view to depend on the value of something else:<\/p>\n<pre aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> (age &lt; <span class=\"hljs-number\">21<\/span>) {\n  slideView.pushView(<span class=\"hljs-string\">'#soda'<\/span>);\n} <span class=\"hljs-keyword\">else<\/span> {\n  slideView.pushView(<span class=\"hljs-string\">'#beer'<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Here&#8217;s a working example that uses <a href=\"http:\/\/momentjs.com\/\">Moment.js<\/a> and your browser to determine the weekday of a date you provide. The result is populated <em>before<\/em> the view transitions, and only if <code>Moment.isValid()<\/code> returns <code>true<\/code>:<\/p>\n<p data-height=\"300\" data-theme-id=\"0\" data-slug-hash=\"bHmxh\" data-default-tab=\"result\" class='codepen'>See the Pen <a href='http:\/\/codepen.io\/tylersticka\/pen\/bHmxh'>Weekday (SimpleSlideView Demo)<\/a> by Tyler Sticka (<a href='http:\/\/codepen.io\/tylersticka'>@tylersticka<\/a>) on <a href='http:\/\/codepen.io'>CodePen<\/a>.<\/p>\n<p><script async src=\"\/\/codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<p>For more info on what you can do with SimpleSlideView and JavaScript, refer to the <a href=\"https:\/\/github.com\/cloudfour\/SimpleSlideView\">documentation on GitHub<\/a>. If you&#8217;re using SimpleSlideView in your own projects, <a href=\"mailto:info@cloudfour.com\">we&#8217;d love to hear about it<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In July we released SimpleSlideView, a jQuery and Zepto plugin for simple, responsive sliding views (here&#8217;s a demo). In the last few months, I&#8217;ve noticed an uptick in developers emailing to ask a variation of this question: How do I change or stop a view transition based on user input? It&#8217;s a great question, and [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[240,3],"tags":[199,200,81,186,189,201,191],"class_list":["post-3806","post","type-post","status-publish","format-standard","hentry","category-javascript","category-mobile-web","tag-forms","tag-howto","tag-javascript","tag-jquery","tag-plugin","tag-simpleslideview","tag-zepto"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/posts\/3806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/comments?post=3806"}],"version-history":[{"count":0,"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/posts\/3806\/revisions"}],"wp:attachment":[{"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/media?parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/categories?post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudfour.com\/wp-json\/wp\/v2\/tags?post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}