Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

jquery - How to create a direct link to any fancybox box

I need that when I click in anything that uses fancybox it generates a specific URL for that, so when I send this link to someone, it opens the specific box I want.

For example: fancybox.net/home when I click in the first image, the link still fancybox.net/home I want that when I click in the image, the URL is generated and appears in address bar like: fancybox.net/home/imageid=1 so when i send fancybox.net/home/imageid=1 to someone it already opens the image in the box

Thanks!

(It is like facebook photos, when you click in any photo, the photo opens in a box but the address bar changes to the image link)

////// UPDATE #1 //////

I did what JFK suggested but after one hour trying i still don't know why the boxes aren't the same.

Look the diference between:

the code:

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
if(window.location.hash) {
$(thisHash).fancybox({
    prevEffect : 'none',
    nextEffect : 'none',
    closeBtn  : false,
    arrows    : true,
    nextClick : true,
    helpers :   { 
            thumbs : {
                width  : 80,
                height : 80
            },
    title : {
            type : 'inside'
            },
    buttons : {}
                },

    afterLoad : function() {
            this.title = (this.index + 1) + ' de ' + this.group.length + '<div id="curti"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.google.com.br&amp;send=true&amp;layout=standard&amp;width=45&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:56px; height:24px;" allowTransparency="true"></iframe></div>';
            }

                }).trigger('click');
 }
 $('.fancylink').fancybox({
    prevEffect : 'none',
    nextEffect : 'none',
    closeBtn  : false,
    arrows    : true,
    nextClick : true,
    helpers :   { 
            thumbs : {
                width  : 80,
                height : 80
            },
    title : {
            type : 'inside'
            },
    buttons : {}
                },

    afterLoad : function() {
            this.title = (this.index + 1) + ' de ' + this.group.length     + '<div id="curti"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.google.com.br&amp;send=true&amp;layout=standard&amp;width=45&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:56px; height:24px;" allowTransparency="true"></iframe></div>';
            }

                });
}); // ready
</script>    

What's wrong in that script?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you still need to have links to your images in the page that opens fancybox like:

<a id="image01" class="fancylink" rel="gallery" href="images/01.jpg" title="image 01">open image 01</a>
<a id="image02" class="fancylink" rel="gallery" href="images/02.jpg" title="image 02">open image 02</a>

... etc.

Notice that I am adding both an ID and a class to each anchor since the only way I have to target them via URL is through their ID ... the class will be used to open those images in fancybox in a regular way once I am on the page so I don't need to write a specific fancybox code for each ID.

then set this script on the page of reference:

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
 if(window.location.hash) {
  $(thisHash).fancybox().trigger('click');
 }
 $('.fancylink').fancybox();
}); // ready
</script>

then you can provide the URL that targets each image like

http://mydomain.com/page.html#image01

or

http://mydomain.com/page.html#image02

etc.

wanna working demo?

http://picssel.com/playground/jquery/targetByID_28jan12.html#image01

http://picssel.com/playground/jquery/targetByID_28jan12.html#image02

NOTE: This code is for fancybox v1.3.x since you used fancybox.net as reference.

UPDATE #1: if you want fancybox options you need to add them in both selectors ID and class like:

<script type="text/javascript">
var thisHash = window.location.hash;
$(document).ready(function() {
 if(window.location.hash) {
  $(thisHash).fancybox({
    padding: 0
    // more API options
  }).trigger('click');
 }
 $('.fancylink').fancybox({
    padding: 0
    // more API options
 });
}); // ready
</script>

Of course, use the right options either for fancybox v1.3.x or 2.x


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...