开发者

Lightbox image / link URL

开发者 https://www.devze.com 2023-02-01 22:36 出处:网络
Basically I have a slightly non-standard implementation of FancyBox. By default you have to include a link to the large version of the image so that the Lightbox can display it. However, in my impleme

Basically I have a slightly non-standard implementation of FancyBox. By default you have to include a link to the large version of the image so that the Lightbox can display it. However, in my implementation, the image link URLs point to a script rather than directly to the image file. So for example, instead of:

<a href="mysite/images/myimage.jpg" rel="gallery">

I have:

<a href="mysite/photos/view/abc123" rel="gallery"> 

The above URL points to a function:

public function actionPhotos($view)
{
    $photo=Photo::model()->find('name=:name', array(':name'=>$view));

    if(!empty($photo))
    {           
        $this->renderPartial('_photo', array('photo'=>$photo, true));
    }
}

The "$this->renderPartial()" bit simply calls a layout file which includes a standard HTML tag to output.

Now when the user clicks on a thumbnail, the above function is called and the large image is displayed in the Lightbox.

Now if the user right clicks on the thumbnail and selects "open in new tab/window" then the image is displayed in the browser as per normal, i.e. just the image. I want to change this so that it displays the image within a layout.

In the above code I can include the following and put it in an IF statement:

$this->render('photos', arr开发者_运维技巧ay('photo'=>$photo));

This will call the layout file "photos" which contains the layout to display the image in.

I have a specific limitation for this - the image URL must remain the same, i.e. no additional GET variables in the URL. However if we can pass in a GET variable in the background then that is OK.

I will most likely need to change my function above so that it calls a different file for this functionality.

EDIT: To demonstrate exactly what I am trying to do, check out the following:

http://www.starnow.co.uk/KimberleyMarren

Go to the photos tab and hover over a thumbnail - note the URL. Click the thumbnail and it will open up in the Lightbox. Next right click on that same thumbnail and select "open in new tab/new window". You will notice that the image is now displayed in a layout. So that same URL is used for displaying the image in the Lightbox and on its own page.

The way StarNow have done this is using some crazy long JavaScript functionality, which I'm not too keen on replicating.


The html link should point to the layout showing the image on a new page by default, e.g.:

<a href="mysite/images/show/123" rel="gallery">

Before the lightbox opens, append a query string to the url in order to distinguish it from the normal link and load the layout for the lightbox. As soon as the image is loaded in the lightbox, change the link back to its original state.

$("a[rel=gallery]").fancybox({
'onStart': function (selectedArray, selectedIndex, selectedOpts) {
    var el = $(selectedArray[selectedIndex]);
    el.attr('href', el.attr('href') + '?mode=lightbox');
},
'onComplete': function (currentArray, currentIndex, currentOpts) {
    var el = $(currentArray[currentIndex]);
    el.attr('href', el.attr('href').split("?")[0]);
}
});

You will then have to process the following link in order to return the lightbox layout:

<a href="mysite/images/show/123?mode=lightbox" rel="gallery">


You should be able to modify the JavaScript function that generates the HTML with the <img /> tag to link the image to such a page. Although, if you are trying to make it so that selecting "Open image in new tab" opens a page like this, then that might be impossible (unless there is some sort of crazy cookie/session implementation to alternate between the image script just passing an image and generating a page, which I think could be possible). To assign a new href for the link to have when you click "Open link in new tab" should be quite possible by just modifying the JavaScript function.

Could you clarify what exactly you are attempting to do? Open link in new tab or open image in new tab?

Edit: It appears that the FancyBox script is changing the href of your link to point directly to the image. You would need to find where in the script it is selecting each link tag with rel="gallery" and replacing the href to point to the images; you will want it to not change the href if you want it left as "mysite/photos/view/abc123", for example.


If you need the same functionality the demo site you posted is using, then this is easy to achieve, but keep in mind that the site is NOT using the same URL for both the pop-up and the standalone image page.

Click on any thumbnail with Firebug console is open, you'll notice that it's making an Ajax request to get the image from a different URL! which is an obvious behavior.

http://www.starnow.co.uk/profile/PhotosTrackView.aspx?photo_id=2129864

While the link is pointing to:

http://www.starnow.co.uk/KimberleyMarren/photos/2129864/

you see your links should point to the correct image page, in case of JS disabled browsing or right clicking (as you mentioned) AND using JS to override the link default behavior (which is redirecting you to the image page).

So for example you can have a method that will generate your image layout/page, and this should be used as href; and override the click event of the link to call a similar method (using ajax) but this time it'll retrieve the image itself to use it in your lightbox.

0

精彩评论

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