Magnific popup cannot read property top of undefined năm 2024
Here you can find the guide on how to use Magnific Popup. Besides this docs page, you can play with examples on CodePen. If you’ve found any mistake in this site or you know how to improve some part of this documentation - please commit on GitHub. Show
Please ask general questions through Stack Overflow tagged with
9. If you’re looking for touch-friendly popup just for images, PhotoSwipe might be a better choice.
Including filesYou can get Magnific Popup JS and CSS file from the , from the
0 folder in the GitHub repository, or by compiling it yourself with Grunt.
It’s not required, but we recommend placing CSS files in
1 and JavaScript files and initialization code in the footer of your site (before the closing
2 tag). If you already have
3 on your site, don’t include it a second time, or use
4 mode. Optionally, you can include Zepto.js instead of jQuery, or choose which one to include based on browser support. Popup initialization code should be executed after document ready, for example:
There are three ways to initialize a popup: 1. From an HTML element
2. From a group of elements with one parentSame as first one, but use this method if you are creating a popup from a list of elements in one container. Note that this method does not enable gallery mode, it just reduces the number of click event handlers; each item will be opened as a single popup. If you wish to enable gallery, add the
5 option.
3. From the ‘items’ optionThe
6 option defines data for the popup item(s) and makes Magnific Popup ignore all attributes on the target DOM element. The value for
6 can be a single object or an array of objects.
Play with this example on CodePen. Content TypesBy default, Magnific Popup has four types of content:
8,
9,
0, and
1. There is no any “auto-detection” of type based on URL, so you should define it manually. The type of a popup can be defined in a two ways:
The second option always overrides the first, so you may initialize popups with multiple content types from one call.
0 is the default content type (from v0.8.4), so you may skip its definition. The source of the the popup content (for example, a path to an image, a path to an HTML file, a path to a video page) can be defined in a few ways: Method 1: From the
9 attribute:
Method 2: From the
0 attribute (overrides the first method):
Method 3: From the
6 option
If you want to modify how the source is parsed, you may hook into the
2 callback. For example:
0 Image TypeThe path to the image must be set as the main source if you selected this type. If your popup doesn’t have an image source and doesn’t have an image that shouldn’t be preloaded (and retina-ized, etc.), use the
0 type.
1 Please note that Magnific Popup doesn’t implement any JavaScript-based client-side caching for images. So make sure that your server so the image won’t be downloaded each time. Iframe TypeBy default Magnific Popup supports only one type of URL for each service:
2 But you can extend it and make it support absolutely any URL or any other service (view example that adds Dailymotion support). Iframe options:
3 Inline TypeTo create popup from inline element you need to:
4 is required to hide the popup from the page.
4
5). Close button will be automatically appended inside (if
6).
5
7 in our case).
6
7 Here are some other ways to initialize popup:
8 I have created two examples on CodePen that will help you better understand how it works:
Ajax TypeTo create such type of popup, first of define the path to the file that you wish to display and select
1 type of the popup. Popup itself should be styled in exactly the same way as an . Important note! The contents of the file that you load is already a popup itself, so there must be only one root element.
9
0 Note that path to the file that will be loaded should have the same origin (e.g. be on the same domain), learn more. Ajax options:
1 To modify content after it’s loaded, or to select and show just specific element from loaded file, there is a
9 callback:
2 OptionsOptions should be passed to the initialization code and separated by comma, e.g.:
3 Options for specific modules are explained in their sections of documentation (e.g. related to text are in , related to gallery are in . Here you can find the list of general options: disableOn
0 If window width is less than the number in this option lightbox will not be opened and the default behavior of the element will be triggered. Set to
1 to disable behavior. Option works only when you initialize Magnific Popup from DOM element. Can also accept Function as a parameter, which should return
2 if lightbox can be opened and
3 otherwise. For example:
4 key
0 “Key” option is a unique identifier of a single or a group of popups with the same structure. If you will not define it, DOM elements will be created and destroyed each time when you open and close popup. You may (and should) set an equal key to different popups if their markup matches. By markup I mean options that change HTML structure of the popup (e.g. close icon placement and HTML code of it). For example: you have many popups that show title, some text and button - you may use one key for all of them, so only one instance of this element is created. Same for popup that always contains image and caption. You can delete cached templates like so:
5 midClick
3 If set to
2 lightbox is opened if the user clicked on the middle mouse button, or click with Command/Ctrl key. Option works only when you initialize Magnific Popup from DOM element. mainClass
7 String that contains classes that will be added to the root element of popup wrapper and to dark overlay. For example
8, can also contain multiple classes -
9. preloader
2 Preloader in Magnific Popup is used as an indicator of current status. If option enabled, it’s always present in DOM only text inside of it changes. Below you can see explanation of CSS names that are applied to container that holds preloader and content area depending on the state of current item:
6 For example, if you want your error message to be in red add such CSS:
7 You can trigger change of status manually by calling
1. focus
7 String with CSS selector of an element inside popup that should be focused. Ideally it should be the first element of popup that can be focused. For example
3 or
4. Leave empty to focus the popup itself. closeOnContentClick
3 Close popup when user clicks on content of it. It’s recommended to enable this option when you have only image in popup. closeOnBgClick
2 Close the popup when user clicks on the dark overlay. closeBtnInside
2 If enabled, Magnific Popup will put close button inside content of popup, and wrapper will get class
8 (which in default CSS file makes color of it change). If markup of popup item is defined element with class
9 it will be replaced with this button, otherwise close button will be appended directly. showCloseBtn
2 Controls whether the close button will be displayed or not. enableEscapeKey
2 Controls whether pressing the escape key will dismiss the active popup or not. modal
3 When set to
2, the popup will have a modal-like behavior: it won’t be possible to dismiss it by usual means (close button, escape key, or clicking in the overlay). This is a shortcut to set
04,
05,
06, and
07 to
3. alignTop
3 If set to
2 popup is aligned to top instead of to center. (basically all this option does is adds
11 CSS class to popup which removes styles that align popup to center). index
0 Used for gallery. Defines starting index. If popup is initialised from DOM element, this option will be ignored. fixedContentPos
13 Popup content position. Can be
14,
2 or
3. If set to
2 - fixed position will be used, to
3 - absolute position based on current scroll. If set to
14 popup will automatically disable this option when browser doesn’t support fixed position properly. fixedBgPos
13 Same as an option above, but it defines position property of the dark transluscent overlay. If set to
3 - huge tall overlay will be generated that equals height of window to emulate fixed position. It’s recommended to set this option to
2 if you animate this dark overlay and content is most likely will not be zoomed, as size of it will be much smaller. overflowY
13 Defines scrollbar of the popup, works as overflow-y CSS property - any CSS acceptable value is allowed (e.g.
13,
25,
26). Option is applied only when fixed position is enabled. There is no option
27, but you may easily emulate it just via CSS. removalDelay
1 Delay before popup is removed from DOM. Used for the . closeMarkup
29 Markup of close button. %title% will be replaced with option
30. prependTo
31 The DOM element to which popup will be added. Useful when you’re using ASP.NET where popup should be inside
32. Option available since 2013/12/04. autoFocusLast
2 If set to
2 last focused element before popup showup will be focused after popup close. Option available since 2015/12/16. GalleryThe gallery module allows you to switch the content of the popup and adds navigation arrows. It can switch and mix any types of content, not just images. Gallery options:
8 Example:
9 Multiple galleriesTo have multiple galleries on a page, you need to create a new instance of Magnific Popup for each separate gallery. For example
0
1 You don’t necessarily need to use
35 option, it can be just
36. Lazy-loadingLazy-loading option preloads nearby items. It accepts an array with two integers. The first is the number of items to preload before the current. The second is the number of images to preload after the current. For example,
37 will load 3 next items and 1 that is before current. These values are automatically switched based on direction of movement. By default all what it does is just searches for an image tag and preloads it with JavaScript. But you can extend it and do your custom preloading logic with help of
38 event, like so:
2 “Preload” option can be changed dynamically. To disable it set
39. AnimationAnimation can be added to any example. For Ajax based popup content animation is fired only after content is loaded. After popup is opened popup wrapper and background overlay get class
40. Before popup is removed they get class
41. For example:
3 Then just play with CSS3 transitions:
4 Please use animation wisely and when it’s really required. Do not enable it when your popup may contain large image or a lot of HTML text. Retina“Retina” module allows you to display high-resolution images on high-dpi screens with different
42. Module works only with “image” type and only when
43. First of prepare two sets of images. Default supported syntax requires
44 at the end of the image file name, e.g.:
45 >
46. Then initialize popup as usual and add
47 in retina set of options.
5
6 View example of retina popup on CodePen. Zoom effectZooming only works for images, for now. To add zooming effect, first of make sure that you included “zoom” module to Magnific Popup build (since v0.9.1.). Then follow steps:
7 You need to make sure that ratio of your thumbnail matches the ratio of the big image, to avoid “jumps” at the end of zoom-out animation.
48 options:
8
9 Zoom module adds
49 callback, which fires when zoom-in animation is finished. APIThere is a much bigger list of public events, methods and variables than provided here which is used for module development, look inside code or type in console
50 to find them, if you think that something should be added to docs - please submit commit. EventsYou can define callbacks in
51 option. Besides that, all Magnific Popup events are also dispatched using
52 on target element (or to document if the element doesn’t exist).
0 List of callbacks. In each callback
53 is
54, so you can execute methods (
56).
1 Public methods
2 You may also call ANY method via
57 after you initialized the popup, for example:
3 You may also open the popup directly at initialization:
4 Public propertiesMost properties are available only after the popup is opened. Only most used are listed here.
5 TranslatingInternationalization of Magnific Popup is very simple, all you need is to extend default settings object with new values, or just pass options to your initialization code. If you’re making some public plugin or theme, it’s strongly recommended to use only second method to avoid conflicts. Some properties contain %keys% that should not be translated, but may be reordered or removed.
6 Same thing, but applied only to specific slider:
7 FAQHow to place gallery navigation arrows “inside” the image?See example on CodePen. How to override some function without modifying the source files?Rewrite the function that you wish to modify by editing Magnific Popup object, you can access it like so
54. For example to override function that goes to “next” item in gallery:
8 You may override any public function, just note that this change applies globally. How to add spinner indicator instead of “Loading…” text?Just style element with class
59. Example on CodePen. Another example (if you want to show image only after its fully loaded). Known issuesWhen popup is opened scrollbar of window disappears and creates empty space or shifts some fixed-positioned menu (or whatever)Solution 1: add option to force the scrollbar. Solution 2: use open/close popup callbacks to apply custom styling to menu that behaves incorrectly. Refer to this discussion on GitHub. Make Magnific Popup better!Improve this documentation page (simply submit commit via GitHub). Any improvements, including your own CodePen examples are very welcome. And, lastly, don’t forget to star the script on GitHub: How do you fix undefined properties that cannot be read?The error can be fixed by creating a variable to hold the name of the file. This will fix the error and will not change anything in your application. How to resolve cannot read properties of undefined reading 0 ')?It's often easier to simply check for the particular array-like types that you're actually using. This is the easiest solution to mitigate the Cannot read property '0' of undefined error. You can also use it to check if an argument of a function is undefined or not. |