Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>border-radius</h2> <p>CSS3 <code>border-radius</code> can be used to create a round div with a background image which serves as the image spotlight. The spotlight can be overlaid on top of the main image, and positioned based on the mouse coordinates. <a href="http://jsfiddle.net/CkJXJ/3/" rel="noreferrer">JSFiddle Demo</a></p> <p>Although there's no natural way to soften the edges of the spotlight in CSS3 -- which would require support for adding an opacity gradient to arbitrary content -- it can be simulated using a staggered set of elements with increasing radius and decreasing opacity. <a href="http://jsfiddle.net/ga2D2/3/" rel="noreferrer">Updated demo with softened edges</a></p> <p>In the <a href="http://jsfiddle.net/ga2D2/3/" rel="noreferrer">updated demo</a>, the size and softness of the the spotlight can be adjusted using the following variables:</p> <pre><code>var spotlightDiameter = 150; // Base size (not including the soft edge) var numSpotlightLayers = 6; // More layers = softer edges var spotlightLayerThickness = 2; // Thinner = the softening is more subtle </code></pre> <p>Here's a <a href="http://jsfiddle.net/bu7rd/" rel="noreferrer">modified demo</a> where the spotlight has noticeable ripples. The thickness of the layers was increased to show more clearly how it works.</p> <p>Below is a simplified version of the code for the initial version with sharp edges.</p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="content"&gt; &lt;div class="spotlight"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.content { position: relative; width: 640px; height: 480px; background: url(desaturated.jpg) no-repeat 0 0; overflow: hidden; } .spotlight { display: none; position: absolute; background: url(overly_saturated.jpg) no-repeat 0 0; } </code></pre> <p><strong>jQuery</strong></p> <pre><code>var spotlightDiameter = 150; // Update the spotlight position on mousemove $('.content').on('mousemove', function(e){ var center = {x: e.pageX - this.offsetLeft, y: e.pageY - this.offsetTop}; var x = center.x - (spotlightDiameter &gt;&gt; 1); var y = center.y - (spotlightDiameter &gt;&gt; 1); $('.spotlight').css({left: x + 'px', top: y + 'px', backgroundPosition: -x + 'px ' + -y + 'px'}).show(); }); // Hide the spotlight on mouseout $('.content').on('mouseout', function(e){ $('.spotlight').hide(); }); // Initialize the spotlight $(document).ready(function(){ $('.spotlight').width(spotlightDiameter + 'px') .height(spotlightDiameter + 'px') .css({borderRadius: (spotlightDiameter &gt;&gt; 1) + 'px'}); }); </code></pre> <h2>Alternative implementations</h2> <p>This could also be implemented using HTML5 Canvas or SVG. Below is a browser-support comparison of the different approaches:</p> <ul> <li><code>border-radius</code>: Not supported by <a href="http://caniuse.com/#search=border-radius" rel="noreferrer">IE8 or earlier</a>.</li> <li>HTML5 Canvas: Not supported by <a href="http://caniuse.com/#search=canvas" rel="noreferrer">IE8 or earlier</a>.</li> <li>SVG: Not supported by <a href="http://caniuse.com/#search=svg" rel="noreferrer">IE8 or earlier</a>, or <a href="http://caniuse.com/#search=svg" rel="noreferrer">Android 2.3 or earlier</a> (which is still most of the <a href="http://developer.android.com/about/dashboards/index.html" rel="noreferrer">Android marketshare</a>).</li> </ul> <p>In short, IE8 and earlier is not an option for any of these approaches, and if Android support is needed, that limits the choices to <code>border-radius</code> and HTML5 Canvas. Of course, since this is mouse-based, Android support may not be a factor anyhow.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload