开发者

How do I get jQuery to do one thing on mouseover and another thing on click?

I am building a photo gallery that has two ways of viewing images, the first is on mouse over so that it brings up a tooltip preview image and the second is on click to take the viewer to a new page with the preview image and more information about the image, much like you would have on iStock Photo: http://www.istockphoto.com/stock-photo-2159036-hot-air-balloons.php

You can view my development page here: http://ee.rouviere.com/%5Ehtml/photography/index.html

Currently I am using fancybox which works nicely to bring up the preview image when you click on the thumbnail. However, I would like to change this so that it comes up when you mouse over the thumbnail. Much like it does on this page: http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/ In fact this would be perfect if when you click on the image it takes you to an image detail page instead of just loading the image in a new window.

C开发者_运维百科urrently the fancybox jQuery code is like this:

<script type="text/javascript">
jQuery(document).ready(function() {
    $("a.view-preview").fancybox({
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic'
    });
});
</script>

I would appreciate any help I can get on this. Thanks!


It sounds like what you want is a tooltip instead of a lightbox (fancybox). Try using a tooltip plugin, something like jQueryTools Tooltip.


Update: I updated the plugin code to work with the following HTML layout. Replace the # in the <a> tag with the page you want to go to when the user clicks on the image. Also, here is a demo.

<ul>
  <li>
    <a href="#">
      <img class="preview" src="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/1s.jpg" data-fullimg="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/1.jpg" data-caption="Lake and a mountain" alt="gallery thumbnail" />
    </a>
  </li>
  <li>
    <a href="#">
      <img class="preview" src="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/2s.jpg" data-fullimg="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/2.jpg" data-caption="Fly fishing" alt="gallery thumbnail" />
    </a>
  </li>
  <li>
    <a href="#">
      <img class="preview" src="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/3s.jpg" data-fullimg="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/3.jpg" data-caption="Autumn" alt="gallery thumbnail" />
    </a>
  </li>
  <li>
    <a href="#">
      <img class="preview" src="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/4s.jpg" data-fullimg="http://www.webinventif.fr/wp-content/uploads/projets/tooltip/02/4.jpg" data-caption="Skiing on a mountain" alt="gallery thumbnail" />
    </a>
  </li>
</ul>

This is the updated script:

/*
 * Image preview script
 * powered by jQuery (http://www.jquery.com)
 *
 * written by Alen Grakalic (http://cssglobe.com)
 *
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

this.imagePreview = function(){
  /* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

  /* END CONFIG */
  $("img.preview").hover(function(e){
    var t = $(this).attr('data-caption');
    var c = (t != "") ? "<br/>" + t : "";
    $("body").append("<p id='preview'><img src='"+ $(this).attr('data-fullimg') +"' alt='Image preview' />"+ c +"</p>");
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast");
  }, function(){
    $("#preview").remove();
  });
  $("img.preview").mousemove(function(e){
    $("#preview")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px");
  });
};

// starting the script on page load
$(document).ready(function(){
  imagePreview();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜