Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

JavaScript

Add viewport tag to head with jQuery

//useful for jsfiddle
$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />');
https://snipt.net/embed/436b83cbffb758473413035d5f34ed3b/
https://snipt.net/raw/436b83cbffb758473413035d5f34ed3b/
436b83cbffb758473413035d5f34ed3b
js
JavaScript
2
2016-12-09T00:39:44
True
False
False
Feb 18, 2014 at 11:15 AM
/api/public/snipt/114793/
add-viewport-tag-to-head-with-jquery
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">//useful for jsfiddle</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;head&#39;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s1">&#39;&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1&quot; /&gt;&#39;</span><span class="p">);</span> </span></pre></div> </td></tr></table>
head, javascript, jquery, viewport

JavaScript

Get the browser viewport & center element

1
2
3
4
5
var viewportWidth = (self.innerWidth || (document.documentElement.clientWidth || (document.body.clientWidth || 0)));
var viewportHeight = (self.innerHeight || (document.documentElement.clientHeight || (document.body.clientHeight || 0)));

myelement.style.left = viewportWidth/2 - myelement.offsetWidth/2 + 'px';
myelement.style.top = viewportHeight/2 - myelement.offsetHeight/2 + 'px';
https://snipt.net/embed/2c461e7e333748bbce7d3a71b36d05b3/
https://snipt.net/raw/2c461e7e333748bbce7d3a71b36d05b3/
2c461e7e333748bbce7d3a71b36d05b3
js
JavaScript
5
2016-12-06T18:36:50
True
False
False
/api/public/snipt/41059/
get-the-browser-viewport-center-element
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">viewportWidth</span> <span class="o">=</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">innerWidth</span> <span class="o">||</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">clientWidth</span> <span class="o">||</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">clientWidth</span> <span class="o">||</span> <span class="mi">0</span><span class="p">)));</span> </span><span id="L-2"><a name="L-2"></a><span class="kd">var</span> <span class="nx">viewportHeight</span> <span class="o">=</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">innerHeight</span> <span class="o">||</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">clientHeight</span> <span class="o">||</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">clientHeight</span> <span class="o">||</span> <span class="mi">0</span><span class="p">)));</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="nx">myelement</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">viewportWidth</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="nx">myelement</span><span class="p">.</span><span class="nx">offsetWidth</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="s1">&#39;px&#39;</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="nx">myelement</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">top</span> <span class="o">=</span> <span class="nx">viewportHeight</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="nx">myelement</span><span class="p">.</span><span class="nx">offsetHeight</span><span class="o">/</span><span class="mi">2</span> <span class="o">+</span> <span class="s1">&#39;px&#39;</span><span class="p">;</span> </span></pre></div> </td></tr></table>
browser, dimensions, javascript, js, viewport
Copyrighted, illegal, or inappropriate content? Email [email protected].