如何让 jQuery 在鼠标悬停时执行一件事,在单击时执行另一件事?

发布于 2024-10-12 23:16:21 字数 1073 浏览 1 评论 0原文

我正在构建一个照片库,它有两种查看图像的方式,第一种是鼠标悬停,以便显示工具提示预览图像,第二种是单击将查看者带到带有预览图像和更多信息的新页面关于图像,就像您在 iStock Photo 上看到的一样:http: //www.istockphoto.com/stock-photo-2159036-hot-air-balloons.php

您可以在这里查看我的开发页面:http://ee.rouviere.com/%5Ehtml/photography/index.html

目前我正在使用 fancybox,它可以很好地在以下情况下显示预览图像:您单击缩略图。但是,我想更改此设置,以便当您将鼠标悬停在缩略图上时它会出现。与此页面上的操作非常相似:http://www.webinventif。 fr/wp-content/uploads/projets/tooltip/02/ 事实上,如果当您单击图像时它会将您带到图像详细信息页面而不是仅在新窗口中加载图像,那么这将是完美的。

目前 fancybox jQuery 代码是这样的:

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

我将不胜感激任何我能得到的帮助。谢谢!

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.

Currently 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-10-19 23:16:21

听起来您想要的是工具提示而不是灯箱(fancybox)。尝试使用工具提示插件,例如 jQueryTools Tooltip


更新:我更新了插件代码以使用以下 HTML 布局。将 标记中的 # 替换为用户单击图像时想要转到的页面。另外,这里有一个演示

<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>

这是更新后的脚本:

/*
 * 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();
});

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();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文